Overview

Appendix. PowerShell cheat sheet

This appendix serves as a compact “gotchas” reference for everyday PowerShell use, gathering the small but important syntactic rules and behaviors you’re likely to forget in the moment. It emphasizes that punctuation can mean different things in the interactive shell versus the help documentation, and encourages consulting full help when in doubt for complete details and examples.

In the shell, punctuation carries specific roles: the backtick escapes special characters; the tilde resolves to the user’s home; parentheses control execution order and enclose method arguments; square brackets index arrays and specify types for casting; and curly braces delimit script blocks, define hashtables (with a preceding @), or wrap unusual variable names. Single quotes produce literal strings, while double quotes expand variables and escapes; $ introduces variables; % and ? are aliases for ForEach-Object and Where-Object; > redirects output; arithmetic symbols behave as expected with + also concatenating strings; - prefixes parameters and comparison operators; @ also enables array literals, here-strings, and splatting; & invokes a command; ; chains commands on one line; # starts comments and <# #> marks block comments; = assigns values (including combined forms like +=); | pipes objects; / and \ separate paths (with / also dividing in math); . accesses members, dot-sources scripts, and with .. forms ranges or a parent folder reference; , creates arrays or multi-value arguments; :: accesses static members; and ! is an alias for -not. The caret is generally unused by the shell itself. In help syntax, [ ] mark optional elements, [] after a type (for example, string[]) indicates multiple values are accepted, and < > show expected data types.

PowerShell comparison operators use verb-like forms: -eq, -ne, -gt, -lt, -ge, -le with case-sensitive variants prefixed by c; collection tests include -contains/-notcontains and -in/-notin; logical operators are -not, -and, -or; and utility operators include -join, -split, -replace, -is, -as, the range operator .., and the format operator -f. For custom properties and formatted output, you pass hashtables with keys like Name/Label (n/l) and Expression (e), often referencing the current object with $_; Format-Table additionally honors width, align, and formatstring. Pipeline binding happens in two stages: ByValue first (binding the entire piped object to a parameter that accepts that type from the pipeline), and if that fails, ByPropertyName (matching object property names to parameters that accept pipeline input by property). Explicitly supplying a parameter value on the command line overrides and disables pipeline binding for that parameter.

A.6 When to use $_

This is probably one of the most confusing things about the shell: When is the $_ placeholder permitted? As we learned earlier the $_ is a placeholder for the next object in the pipeline.

This placeholder works only when the shell is explicitly looking for it and is prepared to fill it in with something. Generally speaking, that happens only within a script block that’s dealing with pipeline input, in which case the $_ placeholder will contain one pipeline input object at a time. You’ll run across this in a few places:

  • In the filtering script block used by Where-Object:
  • In the script blocks passed to ForEach-Object, such as the main Process script block typically used with the cmdlet:
  • In the Process script block of a filtering function or an advanced function. Our other book, Learn PowerShell Toolmaking in a Month of Lunches, discusses these.
  • In the expression of a hash table that’s being used to create a custom property or table column. Refer to section 28.4 in this chapter for more details, or read chapters 8, 9, and 10 for a more complete discussion.

In every one of those cases, $_ occurs only within the curly braces of a script block. That’s a good rule to remember for figuring out when it’s OK to use $_.

FAQ

How do I escape spaces or special characters in a PowerShell path?Use the backtick (`) as the escape character or, more simply, quote the path. Examples: Set-Location C:\Program` Files or Set-Location 'C:\Program Files'. Quoting is usually clearer.
What’s the difference between single and double quotes?Single quotes are literal (no variable expansion, no escape sequences). Double quotes expand variables and process backtick escapes. Example: $name = 'World'; "Hello $name`n" produces Hello World plus a newline.
When should I use parentheses, square brackets, or curly braces?- Parentheses ( ): control order of execution and wrap method parameters, e.g., (Get-Content file.txt).
- Square brackets [ ]: type literals and array indexing, e.g., [int]$x, $items[0].
- Curly braces { }: script blocks and hashtables (with @), e.g., Where-Object { $_.Status -eq 'Running' }, @{ Name='N'; Value=1 }.
What does the tilde (~) mean in a path?It represents your home directory (derived from the user profile). Example: Set-Location ~.
What do the symbols %, ?, and & do?- %: alias for ForEach-Object (also the modulus operator in math).
- ?: alias for Where-Object.
- &: invocation operator to execute a command stored in a variable, e.g., $cmd='Get-Date'; & $cmd.
How does file redirection with > work?> redirects output to a file, similar to using Out-File. Example: Get-ChildItem > files.txt. Use >> to append.
What do brackets and angle brackets in help syntax mean?- Square brackets around items indicate optional parts: [-Name <string>].
- Adjacent brackets in a type, like <string[]>, mean the parameter accepts multiple values.
- Angle brackets show the expected type: <int>, <string>, etc.
What’s the difference between -contains and -in?-contains checks if a collection has an item: $list -contains 'a'.
-in checks if an item is in a collection: 'a' -in $list.
They read in opposite directions; use the one that matches your expression naturally. Negations are -notcontains and -notin.
How does pipeline binding ByValue vs ByPropertyName work?PowerShell tries ByValue first: if the piped object’s type matches a parameter that accepts pipeline input ByValue, it binds the whole object. If not, it tries ByPropertyName: it matches properties on the piped object to parameter names that accept ByPropertyName. Use Get-Member to see types/properties and Get-Help <cmdlet> -Full to see pipeline binding. If you supply a parameter explicitly on the command line, that parameter won’t accept pipeline input.
How do I add custom properties or columns with Select-Object/Format-Table?Use a hashtable with name/label and an expression: @{n='UpperName';e={$_.Name.ToUpper()}}. Example: Get-Service | Select-Object @{n='UpperName';e={$_.Name.ToUpper()}}, Status. For table formatting, you can also add keys like width and align with Format-Table.
Which comparison and utility operators should I know?- Comparisons: -eq, -ne, -gt, -lt, -ge, -le; add a leading c for case-sensitive string versions (e.g., -ceq).
- Logical: -not, -and, -or.
- Utilities: -join, -split, -replace, -is, -as, range .. (e.g., 1..5), and format -f (e.g., "{0}, {1}" -f 'Hello','World').

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn PowerShell in a Month of Lunches, Fourth Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn PowerShell in a Month of Lunches, Fourth Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Learn PowerShell in a Month of Lunches, Fourth Edition ebook for free