top of page
comp oper.PNG


##Quick overview of different operators in Powershell

#Equality Operators
$a=10
$b=20


# -eq 'equal to'
# -ne 'not equal to'
# -gt 'greater than'
# -lt 'less than'
# -ge 'greater than or equal to'
# -le 'less than or equal to'

if(1 -gt 0){'True'}


if($a -eq $b){Write-Host '$a is equal to $b'} 
if($a -ne $b){Write-Host '$a is not equal to $b'} 


if($a -gt 9){Write-Host '$a is greater than 10'} 
if($a -lt 11){Write-Host '$a is less than 10'} 

if($a -ge 10){Write-Host '$a is greater than or equal to 10'} 
if($b -le 20){Write-Host '$b is less than or equal to 20'} 

 


#Matching Operators
$OS='Windows'

if($OS -like '*Win*'){ 'Operating system version is Windows'}
if($OS -notlike '*Win*'){ 'Operating system version may be Linux or Mac OS'}

 

$OS='Windows'
if($OS -match 'Win'){ 'Operating system version is Windows'}
if($OS -notmatch 'Win'){ 'Operating system version may be Linux or Mac OS'}

 

#Containment Operators
$fruit_basket=@('Apple','Orange','Banana')
if($fruits -contains 'Apple') {  Write-Host 'The fruit basket contains Apple'}
if($fruits -Notcontains 'Apple') {  Write-Host 'No Apple in fruit basket'}

$fruit_basket=@('Apple','Orange','Banana')
if('Banana' -in $fruit_basket){ Write-Host 'The fruit basket contains Banana'}
if('Cherry' -notin $fruit_basket){ Write-Host 'The fruit basket does not contain Cherry'}

 

# -replace replaces the values from the string 
$string = 'Glass half empty, glass half full.'
$string -replace 'glass','cup'

 


#Type comparison Operators
#The Boolean type operators (-is and -isNot) tell whether an object is an instance of a specified .
$OS='Windows'
if($OS -is [String]){ '$OS is of variable type string'}
if($OS -isnot [datetime]){ '$OS is not of a date time datatype'}

bottom of page