The following useful information is primarily taken from the Windows PowerShell Language Quick Reference (QuadFold.rtf) documentation.

If / Elseif / Else

When executed on the command line, the closing brace must be on the same line as the preceding keyword.

1if (condition) {...} 
2elseif (condition) {...} 
3else {...}</blockquote>

Switch

The variable $_ is available in the script. $_ represents the current value being evaluated. If an array is used in switch, each element of the array is tested.

Example:

1$var = "word1","word2","word3"
2switch -regex ($var) {
3  "word1" {"Multi-match Exact " + $_ }
4  "word2" {"Multi-match Exact " + $_ }
5  "w.*2" {"Pattern match Exact " + $_ }
6  default {"Multi-match Default " + $_ }
7}

Output:

1Multi-match Exact word1
2Multi-match Exact word2
3Pattern match Exact word2
4Multi-match Default word3

Comparison Operators

The following operators can be used in the condition expressions.

Operator Description Example (true)
-eq Equal 1 -eq 1
-ne Not equal 1 -ne 2
-gt -ge Greater than, greater than or equal to 2 -gt 1
1 -ge 1
-lt -le Less than, less than or equal to 1 -lt 2
1 -le 1
-like Like - for text using wildcards “abc.efg” -like “?bc.*”
-notlike Not Like “abc.efg” -notlike “b*”
-contains Contains 1,2,3 -contains 1
-notcontains Not Contains 1,2,3 -notcontains 4

Note:

  • Prepend “i” for a case-insensitive operation. (e.g. -ieq)
  • Prepend “c” for a case-sensitive operation. (e.g. -ceq)

Logical Operators

The following operators can be used for logical operations.

Operator Description Example (true)
-and Logical AND (1 -eq 1) -and (1 -ne 2)
-or Logical OR (1 -eq 1) -or ($FALSE)
-not or ! Logical NOT -not ($FALSE)

Binary Operators

The following operators can be used for binary operations.

Operator Description
-band Binary AND
-bor Binary OR
-bnot Binary NOT

String Operators

The following operators can be used on strings.

Operator Description
+ Concatenate two strings
* Repeat a string some number of times
-f Format a string (.NET format specifiers)
-replace Replace operator
"abcd” -replace “bc”, “TEST"
aTESTd
-match Regular expression match
-like Wildcard matching

Type Operators

The following operators can be used on types.

Operator Description Example (true)
-is Type evaluator $a -is [string]
-as Type converter 100 -as [string]

Command Expansion Operators

The following operators can be used for command expansion.

Operator Description
$( ) Returns null
$(1,2,3) Returns an array containing 1,2,3.
$(Get-Alias a*) Returns evaluation of the expression
@(Get-Alias;Get-Process) Executes the two commands and returns the results in an array

Other Operators

Operator Description Example (true)
, Array constructor
.. Range operator
& Invoke a script block or the name of a command or function $a = “Get-Process"
&$a
$a = { Get-Process | Select -First 2 }
&$a

Boolean Expressions

The following expressions will result in a boolean value of $TRUE or $FALSE.

TRUE FALSE
Any string of length > 0 except the word “false” Empty string or the string “false”
Any number !=0 Any number = 0
Array of length > 1 Array of length 0
Array of length 1 whose element is TRUE Array of length 1 whose element is FALSE
A reference to any object Null
Please leave below any comments, feedback or suggestions, or alternatively contact me on a social network.

comments powered by Disqus