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.
Quick reference
Comments
Refer to the following table:
Line comment |
# |
# This is a line comment |
Block comment |
<# #> |
<# |
Special characters
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 |
Dot-source operator |
. |
. C:\script.ps1 # Execute the script in the current scope (instead of its own scope) |
Tick in PowerShell
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 |
`0 | 0 |
Bell sound |
`a | 7 |
Backspace |
`b | 8 |
New page form feed |
`f | 12 |
Line feed |
`n | 10 |
Carriage return |
`r | 13 |
Horizontal tab |
`t | 9 |
Vertical tab |
`v | 11 |
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"
Common operators
Refer to the following table:
Equal to |
-eq |
1 –eq 1 # Returns $true |
Not equal to |
-ne |
1 –ne 2 # Returns $true |
And |
-and |
$true –and $true # Returns $true |
Or |
-or |
$true –or $true # Returns $true |
Addition and concatenation |
+ |
1 + 1 # Equals 2 |
Subexpression operator |
$( ) |
“Culture is $($host.CurrentCulture)” |
Dropping unwanted output
Refer to the following table:
Assign to null |
$null = Expression |
$null = Get-Command |
Cast to void |
[Void](Expression) |
[Void](Get-Command) |
Pipe to Out-Null |
Expression | Out-Null |
Get-Command | Out-Null |
Redirect to null |
Expression > $null |
Get-Command > $null |
Creating arrays and hashtables
Refer to the following table:
Using the array operator |
@() |
$array = @() # Empty array |
Implicit array |
Value1, Value2, Value3 |
$array = 1, 2, 3, 4 |
Using the hashtable operator |
@{} |
$hashtable = @{} # Empty hashtable |
Strings
Refer to the following table:
Expanding string |
“ “ |
“Value” |
Expanding here-string |
@” “@ |
$one = ‘One’ |
Non-expanding string |
‘ ‘ |
‘Value’ |
Non-expanding here-string |
@’ ‘@ |
@’ |
Quotes in strings |
“ `” “ “ ““ “ ‘ `’ ‘ ‘ ‘‘ ‘ |
“Double-quotes may be escaped with tick like `”.” |
Common reserved variables
Refer to the following table:
Errors |
$Error |
$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 4. |
$FormatEnumerationLimit |
$object = [PSCustomObject]@{ |
Holds data of the current PowerShell host. |
$Host |
$host |
The matches found when using the -match operator. | $Matches |
‘text’ –match ‘.*’ |
The output field separator. The default is a single space. Dictates how arrays are joined when included in an expandable string. |
$OFS |
$arr = 1, 2, 3, 4 |
Current PowerShell process ID. |
$PID |
Get-Process –Id $PID |
Holds the path to each of the profile files. |
$PROFILE |
$profile.AllUsersAllHosts |
PowerShell version information. |
$PSVersionTable |
|
Present working directory. |
$PWD |
$PWD.Path |
Quick commands and hot keys
Refer to the following table:
ise ise <file> |
Opens PowerShell ISE. Opens a file with ISE if a filename is given. |
code code <file or folder> |
If Visual Studio Code is installed (and in %PATH%). Opens the VS Code. Opens a file or folder with the VS Code. |
Get-History history |
Shows command history for the current session. |
<Text><Tab> |
Autocompletes in context. Tab can be used to complete command names, parameter names, and some parameter values. |
#<Text><Tab> |
Autocompletes against history (beginning of the line). Typing #get- and repeatedly pressing Tab will cycle through all commands containing Get- from your history. |
ii | ii is an alias for the invoke-item. Opens the current directory in Explorer. |
start iexplore |
start is an alias for the start-process. Opens Internet Explorer. |
start <name> -verb runas | Runs a process as administrator. |