Work & productivity / QUICK REFERENCE

PowerShell essentials

Discover commands, inspect objects, filter pipelines and work with text and JSON.

32 entries

Discover the environment

Version information
$PSVersionTable

Distinguishes edition and version. [2]

Find matching commands
Get-Command *Service*

Lists available commands matching the pattern. [2]

Command help
Get-Help Get-Process

Help depth depends on installed help content. [2]

Help examples
Get-Help Get-Process -Examples

Uses the installed help examples. [2]

List command parameters
(Get-Command Get-Process).Parameters.Keys

Inspects the available parameter names. [2]

Current location
Get-Location

PowerShell locations can use providers beyond the filesystem. [2]

List files
Get-ChildItem

Lists items in the current provider location. [2]

Change directory
Set-Location 'C:\Work'

This sample Windows path must exist. [2]

Inspect and filter objects

Running processes
Get-Process

Produces process objects. [3]

Object members
Get-Process | Get-Member

Reveals properties and methods. [3]

Choose output properties
Get-Process | Select-Object Name, Id

Keeps selected properties in new output objects. [3]

Sort by memory
Get-Process | Sort-Object WorkingSet64 -Descending

Orders by numeric working-set size. [3]

First ten results
Get-Process | Select-Object -First 10

Limits output; sorting first changes which results are first. [3]

Filter a property
Get-Process | Where-Object { $_.WorkingSet64 -gt 100MB }

$_ represents the current pipeline object. [3]

Count results
Get-Process | Measure-Object

Reports how many objects were received. [3]

Group by a property
Get-Process | Group-Object ProcessName

Groups process objects with the same name. [3]

Text, files and JSON

Read text lines
Get-Content -LiteralPath 'notes.txt'

LiteralPath avoids wildcard expansion. [4]

Read entire text
Get-Content -LiteralPath 'notes.txt' -Raw

Returns one string instead of individual lines. [4]

Search file text
Select-String -LiteralPath 'app.log' -SimpleMatch 'error'

Uses literal matching rather than a regular expression. [4]

Parse JSON
'{"count":3}' | ConvertFrom-Json

Creates objects from JSON data. [4]

Serialize JSON
@{ name = 'Atlas'; count = 3 } | ConvertTo-Json

Set an appropriate -Depth for nested objects. [4]

Export selected CSV fields
Get-Process | Select-Object Name, Id | Export-Csv -Path 'processes.csv' -NoTypeInformation

Writes or replaces a local CSV file. [4]

Test whether a path exists
Test-Path -LiteralPath 'notes.txt'

Does not guarantee a later read will succeed. [4]

Join path components
Join-Path 'data' 'notes.txt'

Uses the provider's path rules. [4]

Small language patterns

Set a variable
$name = 'Atlas'

Single quotes make a literal string. [1]

Expand inside a string
"Hello, $name"

Double quotes expand variables. [1]

Array literal
$items = @(1, 2, 3)

Wraps values in an array expression. [1]

Hash table
$record = @{ Name = 'Atlas'; Count = 3 }

Stores key/value entries. [1]

For each item
foreach ($item in 1..3) { $item * 2 }

Emits each calculated result. [1]

Boolean comparison
3 -gt 2

Uses named comparison operators. [1]

Equality
$name -eq 'Atlas'

String equality is case-insensitive by default; -ceq is case-sensitive. [1]

Catch expected failures
try { Get-Content missing.txt -ErrorAction Stop } catch { 'Read failed' }

-ErrorAction Stop turns this nonterminating error into a catchable one. [1]

Reserved placement, separate from reference controls. No ad network is active.

Updated PowerShell 7 · object pipelines

Sources

  1. PowerShell 101 ↗
  2. Discovering commands ↗
  3. Objects and properties ↗
  4. PowerShell pipelines ↗