Operators

A detailed look on operators.

"." operator

The . operator (dot operator) can be used when you need properties.

The example below will be clear enough to understand what I mean.

PS C:\PwrShell.net> $a = Get-Date
PS C:\PwrShell.net> $a.Day
3
PS C:\PwrShell.net> $a.DayOfWeek
Thursday
PS C:\PwrShell.net> $a.TimeOfDay.Hours
2
		

Arithmetic Operators

Array Operators

A few examples:

PS C:\> $a = 1..10
PS C:\> $a
1
2
3
4
5
6
7
8
9
10

PS C:\> $a = @(1..3)
PS C:\> $a
1
2
3

PS C:\> 7..3
7
6
5
4
3

Using the index number in an array:

PS C:\> $a = 5..7
PS C:\> $a
5
6
7
PS C:\> $a[1]
6

Assignment Operators

Command Expansion Operators

Comparison Operators

A few examples:

PS C:\PwrShell.net> 10 -eq 10
True
PS C:\PwrShell.net> 5 -lt 10
True
PS C:\PwrShell.net> 5 -gt 10
False
PS C:\PwrShell.net> 10 -ge 10
True
PS C:\PwrShell.net> 10 -le 10
True
PS C:\PwrShell.net> 10 -ne 10
False
PS C:\PwrShell.net> 5 -ne 10
True
		

When we compare strings, PowerShell is by default not case-sensitive. You can change it by appending a character to the operator.

Syntax examples of insensitive variants: -imatch, -inotmatch, -ilike, -inotlike, -ieq, -ine, -igt, -ige, -ilt, -ile.

Some examples to show the difference:

PS C:\PwrShell.net> # comparing in default mode
PS C:\PwrShell.net> "Hello" -eq "HELLO"
True
PS C:\PwrShell.net> "HELLO" -eq "HELLO"
True

PS C:\PwrShell.net> # comparing when case-insensitive
PS C:\PwrShell.net> "Hello" -ieq "HELLO"
True
PS C:\PwrShell.net> "HELLO" -ieq "HELLO"
True

PS C:\PwrShell.net> # comparing when case-senstivive
PS C:\PwrShell.net> "Hello" -ceq "HELLO"
False
PS C:\PwrShell.net> "HELLO" -ceq "HELLO"
True
		

Don't forget it's for example -ceq and not -eqc. If you make a mistake your error would look like this:

PS C:\PwrShell.net> "Hello" -eqc "HELLO"
You must provide a value expression on the right-hand side of the '-eq' operator.
At line:1 char:12
+ "Hello" -eqc <<<<  "HELLO"
		

Invoke Operator

Syntax of the invoke operator (&):

PS C:\PwrShell.net> $a = "Get-Process"
PS C:\PwrShell.net> &$a

Logical Operators

A few examples:

PS C:\PwrShell.net> # all conditions must be true		
PS C:\PwrShell.net> if ( (6 -lt 10) -and (15 -gt 10) ) {
>> # your code here
>> }

PS C:\PwrShell.net> # 1 condition must be true
PS C:\PwrShell.net> if ( ($i -gt 10) -or ($i -lt 100) ) {
>> # your code here
>> }

PS C:\PwrShell.net> if ( !(10 -eq 10) ) {
>> # your code here
>> }

PS C:\PwrShell.net> !(10 -eq 10)
False
PS C:\PwrShell.net> -not(10 -eq 10)
False
PS C:\PwrShell.net> (10 -eq 10)
True
		

Redirection operators

String operators

A few examples:

PS C:\PwrShell.net> "string1" + "string2"
string1string2

PS C:\PwrShell.net> $test = "string1"
PS C:\PwrShell.net> $test * 4
string1string1string1string1

PS C:\PwrShell.net> "{0:M}" -f $(Get-Date)
03 april

PS C:\PwrShell.net> $a = 1,2,3,4
PS C:\PwrShell.net> $a
1
2
3
4
PS C:\PwrShell.net> $OFS = ":"
PS C:\PwrShell.net> "$a"
1:2:3:4

PS C:\PwrShell.net> "Houston, we have a problem!" -replace "Houston","HOUSTON"
HOUSTON, we have a problem!

Unary Operators