Conditions
Comparing two variables
In PowerShell we can compare variables equality and check is a value in an array etc. Comparisons will return True
or False
.
Comparison Operator -eq
means equality.
$a, $b = "Jack", "Jake"
$a -eq $b
Comparison Operator -ne
means not equality.
$a, $b = "Jack", "Jake"
$a -ne $b
The full list of Comparison Operators is below.
Flow Control
In PowerShell scripts we can control the flow of the script. Sometimes we do something depending of a condition or conditions. And sometimes we will check if a condition is true
or false
. The If
-clause is classic legendary way to do it. Let's Go!
$a,$b = 'Jack', 'unknown'
if ($a -eq 'Jakke')
{Write-Host "Terve Jakke"}
else
{Write-Host "Hello", $b}
Let's try with array, is a person in the group.
$a = 'Jack'
$myfriends = 'Al', 'Ben', 'Jack', 'Zake'
if ($myfriends -contains $a)
{Write-Host $a, "is my friend"}
else
{Write-Host $a, "is not in my friend list"}
Example with IP Address
#get all ip data from ipconfig application
$myip = ipconfig.exe
# in this context id 13 is my IP address
$myipa = $myip[13]
if($myipa -like '*192.168*'){Write-host "Internal Network"} else{"Not Internal IP"}
Example with comparing numbers
In the example, we compare is a given number less that set limit, and is it in array.
#ask an odd number from user
$input = Read-Host "Give one odd number below ten"
$a = [int]$input
if ($a -lt 10)
{
$odds = 1,3,5,7,9
if ($odds -contains $a)
{$output = "You gave an odd number " + $a}
else
{$output = "Given number " + $a + " is not valid"}
}
else {$output = "Given number " + $a + " is too big!" }
#finally
Write-Host $output
Comparison Operators
PowerShell contains a number of comparison operators, which are used to compare values and to find values that match certain patterns.
Note:
PowerShell doesn't use the traditional comparison operators that you may know from other programming languages. In particular, the "=" operator is purely an assignment operator in PowerShell, while ">" and "<" operators are used for redirection.
Here is a list of comparison operators in PowerShell.
operator | meaning |
---|---|
-eq | Equal to. |
-ne | Not equal to. |
-gt | Greater than. |
-ge | Greater than or equal to. |
-lt | Less than. |
-le | Less than or equal to. |
-Like | Match using the * wildcard character. |
-NotLike | Does not match using the * wildcard character. |
-Match | Matches the specified regular expression. |
-NotMatch | Does not match the specified regular expression. |
-Contains | Determines if a collection contains a specified value. |
-NotContains | Determines if a collection does not contain a specific value. |
-In | Determines if a specified value is in a collection. |
-NotIn | Determines if a specified value is not in a collection. |
-Replace | Replaces the specified value. |
In the following example is tested contains the array of names certain names.
$names = 'Arska', 'Ben', 'Calle', 'David'
$names -contains 'Ben'
$names -contains 'BEN'
$names -contains 'Eric'
Logical Operators
You can se logical operators (-and, -or, -xor, -not, !) to connect conditional statements into a single complex conditional. For example, you can use a logical -and operator to create an object filter with two different conditions. Or you can use a logical -or operator to test is either one of conditions true.
In the following example we find out is IP-address Internal or not.
#IPs to test are they internal or not
$ips = '192.168.1.1', '10.10.1.1', '204.203.1.1'
foreach ($ip in $ips) {
if ($ip -like '192.168*' -Or $ip -like '10.10*')
{Write-host "$ip is Internal Network"}
else{"$ip is not Internal IP"}
}