Share via


PowerShell: Using Operators in PowerShell Scripting

This TechNet Wiki article gives basic information about using Operators in PowerShell
Execute the below code to get available operators in PowerShell

help -Name '*operators*' | Select Name
#outPut
about_Arithmetic_Operators
about_Assignment_Operators
about_Comparison_Operators
about_Logical_Operators
about_Operators
about_Type_Operator

Learn Operators


help about_Arithmetic_Operators -Detailed
help about_Assignment_Operators -Detailed
help about_Comparison_Operators -Detailed
help about_Logical_Operators -Detailed
help about_Operators -Detailed
help about_Type_Operator -Detailed

Let's see few demo of Operators in PowerShell


Range Operators


1..10 | %{"PowerShell"}

Replace Operator


"Chendrayan V" -replace 'V' , 'Venkatesan'

With Out Operators

$x = "Chen V"
$x.ToString().Replace('V','Venkatesan')

("Chen V").ToString().Replace('V','Venkatesan')

Split Operator


'Chen V' -split ' '
"1;2;3;4;5" -split ';'

Join Operator


1..10 -join '-'

Adding Values to Array using Operator

$array = 1..10
$array += 11..20
$array

IS Operator


"Chen" -is [int]

1 -is [int]

AS Operator


1 -as [int]
#OutPut
1
1 -as [ipaddress]
#OutPut
Address            : 1
AddressFamily      : InterNetwork
ScopeId            : 
IsIPv6Multicast    : False
IsIPv6LinkLocal    : False
IsIPv6SiteLocal    : False
IsIPv6Teredo       : False
IsIPv4MappedToIPv6 : False
IPAddressToString  : 1.0.0.0

Like and Contains Operator


'PowerShell' -like '*Shell'
#Returns True as Output

1..10 -contains 1
#Returns True as Output

1..10 -contains 19
#Returns False as Output

Enjoy PowerShell