Operators

Operators in PowerShell

Comparison operator

Operators Meaning Comment
-eq EQuals ==
-ne Not Equals !=
-gt Greater Than >
-ge Greater than or Equal >=
-lt Less Than <
-le Less than or Equal <=

-eq

PS> 2 -eq 2
True
PS> 1 -eq 2
False
PS> 1,2,3 -eq 2
2

-ne

PS> 2 -ne 2
False
PS> 1 -ne 2
True
PS> 1,2,3 -ne 2
1
3

-gt

PS> 1 -gt 1
False
PS> 2 -gt 1
True
PS> 1,2,3 -gt 1
2
3

-ge

PS> 1 -ge 1
True
PS> 2 -ge 1
True
PS> 1,2,3 -ge 1
1
2
3

-lt

PS> 1 -lt 1
False
PS> 1 -lt 2
True
PS> 1,2,3 -lt 2
1

-le

PS> 1 -le 1
True
PS> 1 -le 2
True
PS> 1,2,3 -le 2
1
2

Array Comparison Operators

Operators Meaning
-contains True if the left-hand array contains the right-hand value
-notcontains if the left-hand array contains the right-hand value, False
-in if the left-hand side value is contained in the right-hand side array, True
-notin if the left-hand side value is contained in the right-hand side array, False

-contains

PS> 1,2,3 -contains 2
True
PS> 1,2,3 -contains 4
False

-notcontains

PS> 1,2,3 -notcontains 2
False
PS> 1,2,3 -notcontains 4
True

-in

PS> 2 -in 1..10
True

-notin

PS> 2 -notin 1..10
False

Type comparison operators

Operator Meaning
-is if the type of the left-hand side and the right-hand side match, True
-isnot if the type of the left-hand side and the right-hand side match, False

-is

PS> 'abc' -is [String]
True
PS> 'abc' -is [Int]   
False
PS> 'abc'.GetType()   

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

-isnot

PS> 'abc' -isnot [String]
False
PS> 'abc' -isnot [Int]   
True

String comparison

In PowerShell, string comparisons are not case-sensitive by default.
To make it case-sensitive, use -ceq.
To make it explicitly case-insensitive, use -ieq.

Operator Meaning
-ceq case-sensitive (Case sensitive EQual)
-eq uncase-sensitive
-ieq uncase-sensitive (Insensitive EQual)

-ceq

PS> "PowerShell" -ceq "powershell"
False

-eq

PS> "PowerShell" -eq "powershell" 
True

-ieq

PS> "PowerShell" -ieq "powershell"
True

Sstring operators

Operator Meaning
-like Match using wildcard character (*)
-notlike Mismatch using the wildcard character (*)
-match Match using regular expression
-notmatch Mismatch with regular expression
-replace Replace string with regular expression
-split Split a string with a regular expression
-join Join strings
-f Formatting operator

-like

PS> "PowerShell" -like "*shell" 
True

-notlike

PS> "PowerShell" -notlike "*shell"
False

-match

Example1 Example of extracting a string whose first letter starts with “power”

PS> "PowerShell", "ShellPower" -match "^power" 
PowerShell

Example2 Checking the $Matches variable

$Matches variable stores the results of regular expression matches.
The match results are stored in ascending order from left to right.

PS> 1234 -match '(.)(.)(.)(.)'
True
PS> $Matches

Name                           Value
----                           -----
4                              4
3                              3
2                              2
1                              1
0                              1234

Exemple3 Extracting user and id values

PS> 'strings user=powershell, id=100 strings' -match '(?:user=)(?<user>\w+), (?:id=)(?<id>\d+)'      
True
PS> $Matches

Name                           Value
----                           -----
id                             100
user                           powershell
0                              user=powershell, id=100

# I got the user and id!
PS> $Matches.user
powershell
PS> $Matches.id
100
PS>

The regular expression pattern characters used in the above example are as follows.

Pattern character Meaning
?:pattern Sub-expressions
?<name>pattern Label the value that matches patter with name
\w 1 word character
\d 1 numeric character
+ Repeats one or more of the preceding characters

-notmatch

PS> "PowerShell", "ShellPower" -notmatch "^power"
ShellPower

-replace

Example1 Example of simple string substitution

PS> "PowerShell" -replace "Shell", "Point"
PowerPoint

# Example of extension conversion
PS> Get-ChildItem *.txt | Rename-Item -NewName { $_.name -replace '\.txt$','.log' }

Example2 Example of using a string before conversion as a pattern character after conversion

You can use a pre-conversion string that matches a pattern as a post-conversion pattern character.
This is specified as $number based on the order in which the pattern was matched.
In the following example, the order of the strings is switched.

PS> 'PowerShell' -replace '(Power)(Shell)', '$2$1'
ShellPower

Example 3 Example of naming a matching pattern and using it as a pattern character after conversion

The string matched by the pattern ?<name> is given a name and used in ${name}.

PS> 'PowerShell' -replace '(?<power>Power)(?<shell>Shell)', '${shell}${power}'
ShellPower

-split

PS> -split "Power Shell"
Power
Shell
PS> "Power,Shell" -split ","
Power
Shell

-join

# Failure example
PS> -join "a", "b", "c"
a
b
c

# Success Example
PS> -join ("a", "b", "c")
abc

PS> "a", "b", "c" -join ""
abc

# Combine strings using the specified string
PS> "Power", "Shell" -join ","
Power,Shell

-f

PS> "{0}{1}" -f "Power", "Shell"
PowerShell

Other formatting examples

# Dec => Hex
PS> '0x{0:x}' -f 255
0xff
PS> '0x{0:X}' -f 255
0xFF

# Display with zero
PS> '{0:d8}' -f 255
00000255

# Padding. Left-justified and right-justified
PS> '|{0,-8}|' -f 255
|255     |
PS> '|{0,8}|' -f 255 
|     255|

However, when it comes to Padding, the
PadLeft and PadRight methods might be easier to understand.

# Example of using the PadLeft method
PS> (255).ToString().PadLeft(8)
     255
PS> (255).ToString().PadLeft(8," ")
     255
PS> (255).ToString().PadLeft(8,"0")
00000255

Logical operators

Operator Meaning
-and True when both the left and right sides are true
-or True when either the left-hand side or the right-hand side is true
-xor True when the left and right sides are different
-not flipping the right-hand side
-or -same as above

-and

PS> (1 -eq 1) -and (1 -eq 2)
False

-or

PS> (1 -eq 1) -or (1 -eq 2)
True

-xor

PS> (1 -eq 1) -xor (2 -eq 2)
False

-not

PS> -not (1 -eq 1)
False

!

PS> !(1 -eq 1)
False

& Call operator

By prefixing & to a script or script block, you can execute the operation.

Example1

PS> $command = "Get-Date"     
PS> $command                  
Get-Date
PS> & $command                

20201125 水曜日 16:26:11

Example2

PS> {Get-Date}
Get-Date
PS> & {Get-Date}

20201125 水曜日 16:29:48

Example3

PS> Get-Content ./script.ps1
Get-Date
PS> & ./script.ps1

20201125 水曜日 16:31:38

.. Range Operator

PS> 1..3    
1
2
3
PS> 1..-1
1
0
-1
PS> 'a'..'c'
a
b
c
PS> 'A'..'C'
A
B
C

Ternary operator a ? b : c

Available since PowerShell7.

# Syntax
<condition> ? <if-true> : <if-false>
# Example of using the ternary operator to output whether Year is 'Odd' or 'Even'.
PS> (Get-Date).ToString("yyyy") % 2 ? 'Odd' : 'Even'
Even

Pipeline chain operators && and ||

Available since PowerShell7.

&&

If the left command succeeds, the right command is executed.

PS> Write-Output 'Success' && Write-Error 'Error'
Success
Write-Error: Error

PS> Write-Error 'Error' && Write-Output 'Success'
Write-Error: Error

||

If the command on the left fails, execute the command on the right.

PS> Write-Output 'Success' || Write-Error 'Error'
Success
PS> Write-Error 'Error' || Write-Output 'Success'
Write-Error: Error
Success

Null-coalescing operator, Null-coalescing assignment operator

Available since PowerShell7.

?? Null-coalescing operator

If the left hand operand is null, returns the right hand operand.
If the left hand operand is not null, returns the left hand operand.

# $num is $null
PS> $num
PS> $num ?? 100
100

# $num is not $null
PS> $num = 1
PS> $num ?? 100
1

??= Null-coalescing assignment operator

Can be assigned only if the left-hand operand is null.

# $num is $null
PS> $num = $null
PS> $num ??= 100
PS> $num
100

# $num is not $null
PS> $num = 1
PS> $num ??= 100
PS> $num
1

YouTube

Click here for a video explanation.

About Operators | Microsoft Docs

About Comparison Operators | Microsoft Docs

About Regular Expressions | Microsoft Docs

About Split | Microsoft Docs

About join | Microsoft Docs

What’s New in PowerShell 7.0 | Microsoft Docs

Suggestion: implement ternary conditionals | GitHub PowerShell/PowerShell

Last modified October 11, 2022: update (8216439)