There is a wide variety of quick references available for PowerShell. This particular reference is intended to kick-start the book, as a lot of this is either not explicitly explained or used often before in a more detailed explanation.
Refer to the following table:
Line comment |
| # This is a line comment |
Block comment |
| <# This is a block or multi-line comment #> |
Refer to the following table:
Statement separator |
| Get-Command Get-Process; Get-Command Get-Help |
Call operator |
| & ‘Get-Process’ # Invoke the string as a command & { Get-Process –Id $PID } # Invoke the script block |
Dot-source operator |
| . C:\script.ps1 # Execute the script in the current scope (instead of its own scope) |
PowerShell uses a tick as a multipurpose escaped
character.
A tick may be used as a line continuation character. Consider the following example:
'one' -replace 'o', 't' ` -replace 'n', 'w' ` -replace 'e', 'o'
When using a tick to split a long statement across several lines, the tick must be the last character (it cannot be followed by a space or any other character).
A tick is used to construct several characters that can be used in strings:
Description | String | ASCII character code |
Null |
|
|
Bell sound |
|
|
Backspace |
|
|
New page form feed |
|
|
Line feed |
|
|
Carriage return |
|
|
Horizontal tab |
|
|
Vertical tab |
|
|
The tab
character, for example, may be included in a string:
PS> Write-Host "First`tSecond"
First Second
Alternatively, the bell sound may be played in the PowerShell console (but not ISE):
Write-Host "`a"
Refer to the following table:
Equal to |
| 1 –eq 1 # Returns $true 1 –eq 2 # Returns $false |
Not equal to |
| 1 –ne 2 # Returns $true 1 –ne 1 # Returns $false |
And |
| $true –and $true # Returns $true $true –and $false # Returns $false $false –and $false # Returns $false |
Or |
| $true –or $true # Returns $true $true –or $false # Returns $true $false –or $false # Returns $false |
Addition and concatenation |
| 1 + 1 # Equals 2 “one” + “one” # Equals oneone |
Subexpression operator |
| “Culture is $($host.CurrentCulture)” “Culture is $(Get-Culture)” |
Refer to the following table:
Assign to |
| $null = Get-Command |
Cast to |
| [Void](Get-Command) |
Pipe to |
| Get-Command | Out-Null |
Redirect to |
| Get-Command > $null |
Refer to the following table:
Using the array operator |
| $array = @() # Empty array $array = @(1, 2, 3, 4) |
Implicit array |
| $array = 1, 2, 3, 4 $array = “one”, “two”, “three”, “four” |
Using the |
| $hashtable = @{} # Empty hashtable $hashtable = @{Key1 = “Value1”} $hashtable = @{Key1 = “Value1”; Key2 = “Value2”} |
Refer to the following table:
Expanding string |
| “Value” $greeting = “Hello”; “$greeting World” # Expands variable |
Expanding here-string |
| $one = ‘One’ @” Must be opened on its own line. This string will expand variables like $var. Can contain other quotes like “ and ‘. Must be closed on its own line with no preceding white space. “@ |
Non-expanding string |
| ‘Value’ ‘$greeting World’ # Does not expand variable |
Non-expanding here-string |
| @’ Must be opened on its own line. This string will not expand variables like $var. Can contain other quotes like “ and ‘. Must be closed on its own line with no preceding white space. ‘@ |
Quotes in strings |
| “Double-quotes may be escaped with tick like `”.” “Or double-quotes may be escaped with another quote ““.” ‘Single-quotes may be escaped with tick like `’.’ ‘Or single-quotes may be escaped with another quote like ‘‘.’ |
Refer to the following table:
Errors |
| $Error[0] # The last error |
Formats the enumeration limit. Dictates the number of elements displayed for objects with properties based on arrays. The default is |
| $object = [PSCustomObject]@{ Array = @(1, 2, 3, 4, 5) } $object # Shows 1, 2, 3, and 4 $formatenumerationlimit = 1 $object # Shows 1 |
Holds data of the current PowerShell host. |
| $host $host.UI.RawUI.WindowTitle |
The matches found when using the |
| ‘text’ –match ‘.*’ $matches |
The output field separator. The default is a single space. Dictates how arrays are joined when included in an expandable string. |
| $arr = 1, 2, 3, 4 “Joined based on OFS: $arr” $ofs = ‘, ‘ “Joined based on OFS: $arr” |
Current PowerShell process ID. |
| Get-Process –Id $PID |
Holds the path to each of the |
| $profile.AllUsersAllHosts $profile.AllUsersCurrentHost $profile.CurrentUserAllHosts $profile.CurrentUserCurrentHost |
PowerShell version information. |
|
$PSVersionTable.PSVersion |
Present working directory. |
| $PWD.Path |
Refer to the following table:
| Opens PowerShell ISE. Opens a file with ISE if a filename is given. |
| If Visual Studio Code is installed (and in Opens the VS Code. Opens a file or folder with the VS Code. |
| Shows command history for the current session. |
| Autocompletes in context. |
| Autocompletes against history (beginning of the line). Typing |
|
|
|
|
| Runs a process as administrator. |