elefcode
← All cheat sheets

Tools

PowerShell Cheat Sheet — Commands for Windows Developers

PowerShell passes objects, not text, through its pipeline. These are the commands and patterns developers use most, with the Bash equivalents in mind.

Put it into practice with the matching tool.

Test a -match pattern →

Advertisement

Navigation and files

Get-Location (pwd)Current directory
Set-Location path (cd)Change directory
Get-ChildItem (ls, dir)List files; -Recurse for subfolders, -Force for hidden
Get-Content file (cat)Read a file; -Tail 20 -Wait to follow it
Set-Content f -Value "x" -Encoding utf8Write a file
New-Item -ItemType Directory -Force dirCreate a directory (like mkdir -p)
Copy-Item / Move-Item / Remove-Itemcp / mv / rm (-Recurse for folders)
Test-Path pathDoes a file or folder exist?

Pipeline and objects

| Where-Object { $_.Length -gt 1MB }Filter objects (alias ?)
| Select-Object Name, LengthPick properties; -First 5 like head
| Sort-Object Length -DescendingSort by a property
| ForEach-Object { $_.Name }Run code per item (alias %)
| Measure-Object -LineCount lines (like wc -l)
| Get-MemberShow an object's properties and methods
Select-String "TODO" -Path *.tsSearch text in files (like grep)

Variables and syntax

$name = "Ana"Assign a variable
"Hello $name" / 'literal $name'Double quotes expand; single quotes do not
$env:PATHRead an environment variable
$env:NODE_ENV = "production"Set one for this session
-eq -ne -gt -lt -like -matchComparison operators
cmd1; if ($?) { cmd2 }Run cmd2 only if cmd1 succeeded (&& in PowerShell 7)
`Escape character and line continuation

Processes and system

Get-Process nodeList processes by name (ps)
Stop-Process -Name nodeKill processes (kill)
Get-NetTCPConnection -LocalPort 3000Find what is using a port
Invoke-WebRequest url -OutFile fDownload a file (like curl -o)
Invoke-RestMethod urlCall an API and parse JSON automatically
(Get-Command node).SourceWhere a command lives (which)

Data and help

Get-Content f.json | ConvertFrom-JsonParse JSON into objects
$obj | ConvertTo-Json -Depth 5Serialise objects to JSON
Import-Csv f.csv / Export-CsvRead and write CSV as objects
Get-Help cmd -ExamplesShow usage examples
Set-ExecutionPolicy -Scope CurrentUser RemoteSignedAllow local scripts to run

Related guides

More cheat sheets