Prof. Powershell

Smooth Operator

Operators are the math geeks of the scripting world. Use them to "do the math" in PowerShell.

Like any console, PowerShell has its own set of operators that allow you get accomplish things, such as performing mathematical operations, a comparison or evaluating a logical expression. PowerShell has many operators but you will most likely make use of operators from these groups:

  • Arithmetic
  • Comparison
  • Assignment
  • Logical

The Arithmetic operators are the same math operators you've come to know and love in the past: * + - /

PS C:\> 3.14*5
15.7
PS C:\> 25/5
5
PS C:\> ((17-3)+4)/(6*1.2)
2.5

By default PowerShell is case-insensitive. Although you can make case-sensitive comparisons if you want and PowerShell fully supports regular expressions. PowerShell's comparison operators are a little different than what you may have used in the past. You can't use the mathematical symbols you may be used to. All operators are preceded by a dash:

-eq (equal)
-lt (less than)
-le (less than or equal to)
-gt (greater than)
-ge (greater than or equal to)
PS C:\> $var=5
PS C:\> $var -eq 5
True
PS C:\> $var -lt 4
False
PS C:\> $var -ge 3
True

You likely already know the main assignment operator, the equal sign. This operator is used to assign a value to a variable as I did above. But PowerShell has a few other assignment operators which save a few keystrokes:

+= Increment by value and Assignment
-= Decrement by value and Assignment
*= Multiply by value and Assignment
/= Divide by value and Assignment
%= Modulo by value and Assignment
PS C:\> $var=10
PS C:\> $var+=1
PS C:\> $var
11
PS C:\> $var-=1
PS C:\> $var
10
PS C:\> $var/=2
PS C:\> $var
5

Finally, logical operators are used in more complex evaluations:

PS C:\> ($var -gt 0) -and ($var -eq 10)
True
PS C:\> ($var -gt 110) -and ($var -eq 10)
False
PS C:\> ($var -gt 110) -or ($var -eq 10)
True

When using the -And operator, both expressions must be True for the entire expression to be True. When using the -Or operator, at least one of the expressions must be True for the entire expression to be True.
Once you get used to using -eq instead of = (which is something I still catch myself doing simply out of many years of CMD.exe habit), you'll find PowerShell's operators easy to use and understand.

To learn more about the different classes of operators, at a PowerShell prompt type: help *operator* and then look at each individual help topic.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular