LLI Wiki — My Digital Knowledge Base

PowerShell special characters and tokens

Quick reference for PowerShell comparison operators, escape sequences and special tokens.

A quick reference for PowerShell comparison operators, escape sequences and common special tokens.

Comparison operators

Operator Meaning
-eq Equal to
-ne Not equal to
-lt Less than
-le Less than or equal to
-gt Greater than
-ge Greater than or equal to

Escape sequences

In a string, the backtick character introduces a special character.

Sequence Character
`n New line
`r Carriage return
`t Horizontal tab
`a Alert (beep)
`b Backspace
`f Form feed (printed documents only)
`v Vertical tab (printed documents only)
`0 Null (prefer $null)
`" Double quote
`' Single quote
` Backtick

Special characters and tokens

Symbol Name Role
$ Dollar sign Declares and uses a variable
$_ Dollar underscore Current item in a loop or pipeline
$$ Double dollar Last token entered
$? Dollar and question mark True or False depending on whether the previous command succeeded
$() Dollar and parentheses Sub-expression evaluated inside a double-quoted string
| Pipeline Sends the output of one command to the next
% Percent sign Alias of ForEach-Object
.. Double dot Range of values
:: Double colon Access to a static member of a class
! Exclamation mark Shortcut for -not
? Question mark Alias of Where-Object
` Backtick Continues the command on the next line, or escapes a character
# Pound sign Single-line comment
<# ... #> Angle brackets and pound sign Multi-line comment
& Ampersand Executes a string as a command
@( ) At sign and parentheses Declares an array (separator: comma)
@{ } At sign and braces Declares a hash table (separator: semicolon)
@' ... '@ Here-string without interpolation Multi-line string without variables
@" ... "@ Here-string with interpolation Multi-line string with variables

Examples

Variables and the current item:

$a = "123"
1..3 | ForEach-Object { Write-Host $_ }

Status of the previous command:

SomeCmdLet          # does not exist
$?                  # False

Get-Date
$?                  # True

Sub-expression in a string:

$name = "example"
"Hello, $name, there are $($name.length) characters in your name"
# Hello, example, there are 7 characters in your name
"There are $(2+3) characters in your name"

Range and filter for odd numbers:

1..5
1..5 | ? { $_ % 2 }
# 1 3 5

Static member:

[string]::Equals("a", "b")
# False

Negation and null test:

$a = $null
if (!$a) { Write-Host 'a is null' }

Continuing a command on the next line:

Write-Host `
"Hello, world!"

Here-strings:

@'
$(1+2)
$(3+4)
$(5+6)
'@
# $(1+2) $(3+4) $(5+6)

@"
$(1+2)
$(3+4)
$(5+6)
"@
# 3 7 11

Miscellaneous

Processes with more than 200 handles:

Get-Process | Where-Object { $_.Handles -gt 200 }

Group processes by company and sort:

Get-Process | Group-Object company | Sort-Object name

List process properties, optionally to a file:

Get-Process | Get-Member -MemberType Property
Get-Process | Get-Member -MemberType Property | Out-File Process.txt

Find members of the SMO Backup object containing "Comp":

New-Object ("Microsoft.SqlServer.Management.Smo.Backup") | Get-Member | findstr "Comp"

"Unable to find repository" error

When a module install fails with PackageManagement\Install-Package : Unable to find repository 'https://www.powershellgallery.com/api/v2/', the repository can be registered again:

Register-PSRepository -Name PSGallery1 -SourceLocation https://www.powershellgallery.com/api/v2/ -InstallationPolicy Trusted

Source: Stack Overflow

Notes

Mastodon LLIMastodon SocialFosstodon