PowerShell Operator

An operator is a language element that you can use in a command or expression. Like other programming languages PowerShell also supports multiple operators for different types of operations.

Microsoft has published a detailed article about operators.

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-7.1

Here are the short definition of few commonly used operators and their examples:

#Define String Variable
$Firstname="Bikash"
$Lastname="Biswal"
$Lastvariable="Hello Your Name is $Firstname $Lastname"
#Examples of Operators
#in Operator. Returns True or False if the left hand string exactly match with Right Hand Variable
'Bikash' -in $Firstname
#inn Operator. Returns True or False if the left hand string exactly match with one of the string in Right hand Array variable
'Bikash' -iin $($Firstname,$Lastname)
#Like/notlike Operator. Returns True or False if the right hand string match with left variable value
$Firstname -like 'Bik*' #Wildcard * sign is mandatory to search the string pattern in the variable
$Number1=10
$number2=20
#Less than (lt) Operator. Returns true or false if the comparison variable is less than
$Number11 -lt $number2
#Less than equal to(le) Operator. Returns true or false if the comparison variable is less than or equal with the target variable or value
$Number11 -le 10
$Number11 -le 9
#Equal to(eq) Operator. Returns true or false if the comparison variable is equal with the target variable or value
$Number11 -eq 10 
#Not Equal to(ne) Operator. Returns true or false if the comparison variable is not equal with the target variable or value
$number2 -ne 30

#greater than/ greater than equal to (gt/ge) Operator. Returns true or false if the comparison variable is greater than or equal with the target variable or value
$Number1 -gt 15
$Number1 -ge 9 

Leave a comment