Understanding and using PowerShell IF and ELSE logical operator

PowerShell is a powerful scripting language. You can do nearly anything using PowerShell as Microsoft has put in a lot of effort in designing PowerShell cmdlets for almost all technologies, which include interacting with Windows components, Office 365, and Azure clouds. There are a few logical operators in the PowerShell scripting library that can be used to take an action based on a condition. For example, let’s say you would want to change the home folder path for an Active Directory user with the username Peter. Or you would like to change the path of the department of the user accounts whose location is set to Dallas. This is where these logical operators come handy. In this article, we talk about IF and ELSE statements used in the PowerShell scripting. IF and ELSE are common logical operators used across PowerShell scripting.

When to use IF and ELSE statements

If you have ever done programming in any of the programming languages, you must have used the IF logical operator. You will use IF and ELSE statements when you want to take an action based on the condition of the statement. However, there are two parts in an IF and ELSE statement: IF and ELSE keywords. The logical condition starts with IF and ends with “}”. For example, to start with a condition you will use this PowerShell statement:

IF (“Condition1 -eq Condition2”) { Do This } ELSE { Do This }

As you can see in the above IF statement, if condition 1 matches condition 2, then “Do This” is performed. If condition 1 does not match condition 2, then statements after “ELSE” are executed.

Example 1: Simple IF condition and move

As you can see in this example, we are checking the home path of user accounts in an Active Directory in a particular organizational unit and based on a condition the user account is moved to another organizational unit.
IF and ELSE

#Condition: Check users whose Home Path is set to blank
#Action: Move users whose Home Path is blank to another organizational unit.
$AllUsers = Get-ADUser -Filter * -Property Name,CanonicalName,CN,DisplayName,DistinguishedName,HomeDirectory
ForEach ($User in $AllUsers)
{
$UserDN = $User.DistinguishedName
IF ($User.HomeDrive -eq $null)
{
Move-ADObject -Identity $UserDN -TargetPath "CN=Users,DC=TechGenix,DC=Com"
}
}

As you can see in the above PowerShell script, it collects all users from the Active Directory and checks if the HomeDrive is $NULL using an IF condition. If the condition matches the command to move the user object to another organizational unit is executed.

Example 2: Simple IF condition with ELSE

In example 2, you would like to add an ELSE condition. The ELSE condition is executed when the IF condition does not match.
IF and Else

#Condition: Check users whose Home Path is set to blank.
#Action: Move users whose Home Path is blank to another organizational unit.
#ELSEAction: Export to CSV file whose Home Path is NOT set to blank.
$HomeFolderOk = "C:\Temp\HomeFoldersReport.CSV"
Remove-Item $HomeFolderOk -ErrorAction SilentlyContinue
$STR = "Username, DistinguishedName, HomeDirectory"
Add-Content $HomeFolderOk $STR
$AllUsers = Get-ADUser -Filter * -Property Name,CanonicalName,CN,DisplayName,DistinguishedName,HomeDirectory
ForEach ($User in $AllUsers)
{
$UserDN = $User.DistinguishedName
IF ($User.HomeDrive -eq $null)
{
Move-ADObject -Identity $UserDN -TargetPath "CN=Users,DC=TechGenix,DC=Com"
}
ELSE
{
$STR = $User.Name+","+$UserDN+","+$User.HomeDirectory
Add-Content $HomeFolderOk $STR
}
}

In Example 2 of the above script, we collect all users from the Active Directory and check if the HomeDrive is $NULL using an IF condition. If the condition matches, the command to move the user object to another organizational unit is executed AND if the condition doesn’t match (that means the user’s HomeDrive is NOT set to $NULL) we report these users in a CSV file named HomeFoldersReport.CSV. So as you can see by combining IF and ELSE you are able to execute two actions based on the IF condition.

Example 3: Nested IF conditions

In example 3, we will show you how you can check multiple IF conditions. You may want to use nested IF conditions if you would like to check two properties of an object and then take an action based on the results of condition 1 and condition 2.

#Condition1: Check if the User’s department is Finance
#Condition2: Check if the User’s Telephone Number is set to None
#Action: Action is to move these users to an organizational unit.
$AllUsers = Get-ADUser -filter * -properties Telephonenumber,Department,DistinguishedName
ForEach ($User in $AllUsers)
{
$UserDN = $User.DistinguishedName
$UserDep = $User.Department
$UserTel = $User.Telephonenumber
IF ($UserDep -eq "Finance")
{
IF ($UserTel -eq $null)
{
Move-ADObject -Identity $UserDN -TargetPath "CN=Users,DC=TechGenix,DC=Com"
}
}
}

As you can see in the above PowerShell script, we are using two IF conditions. The first IF condition checks if the user’s department is set to Finance. If yes, then proceed to check the telephone number of the user. If the telephone number is set to $NULL, then move the user to an organizational unit. The above nested IF condition can also be written as shown in below PowerShell script:

IF ($UserDep -eq "Finance" -AND $UserTel -eq $null)
{
Move-ADObject -Identity $UserDN -TargetPath "CN=Users,DC=TechGenix,DC=Com"
}

IF and ELSE: Basic but powerful PowerShell commands

As part of this article, we provided information about IF and ELSE logical operators used in the PowerShell scripting. The IF logical operator is used to take an action if the condition is matched. We provided examples using IF logical condition in different scenarios.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top