PowerShell Cheat Sheet

PowerShell reference with cmdlets, pipeline, objects, scripting, and system administration commands. Copy-ready for Windows admins.

59 entries 8 sections

Files

Command Description Example
List files and directories Get-ChildItem -Recurse -Filter *.txt
Change directory Set-Location C:\Users\Admin
Print working directory Get-Location → C:\Users\Admin
Copy files or directories Copy-Item file.txt backup.txt
Move or rename files Move-Item old.txt new.txt
Delete files or directories Remove-Item -Recurse -Force folder/
Create file or directory New-Item -ItemType Directory -Name 'logs'
Read file content Get-Content log.txt -Tail 10
Write / append to file Set-Content out.txt 'Hello World'
Check if path exists Test-Path C:\config.json → True/False

Variables

Command Description Example
Variable assignment $name = 'Alice'; $count = 42
Get variable type $name.GetType() → System.String
Type casting [int]'42' → 42; [bool]1 → True
Array literal $arr = @('a', 'b', 'c')
Hashtable (dictionary) $h = @{Name='Alice'; Age=30}
Environment variable $env:PATH; $env:HOME
Special variables if ($result -eq $null) { ... }
Current pipeline object Get-Process | Where-Object { $_.CPU -gt 10 }

Strings

Command Description Example
String interpolation (double quotes) "Count: $($arr.Count)"
Literal string (no interpolation) 'No $variables expanded here'
String replacement (regex) 'hello' -replace 'l','r' → 'herro'
Regex matching 'hello' -match '^h' → True
Split and join strings 'a,b,c' -split ',' → @('a','b','c')
Case conversion 'hello'.ToUpper() → 'HELLO'
String methods ' hi '.Trim() → 'hi'

Pipeline

Command Description Example
Send output to next command Get-Process | Sort-Object CPU -Desc
Filter objects in pipeline Get-Service | Where-Object Status -eq 'Running'
Execute for each object 1..10 | ForEach-Object { $_ * 2 }
Select properties or first/last Get-Process | Select-Object Name, CPU -First 5
Sort pipeline objects Get-ChildItem | Sort-Object Length -Desc
Calculate stats (count/sum/avg) 1..100 | Measure-Object -Sum -Average
Group objects by property Get-Service | Group-Object Status
Format output display Get-Process | Format-Table Name, CPU -Auto
CSV import/export Get-Process | Export-Csv procs.csv -NoType
JSON conversion @{a=1} | ConvertTo-Json → '{"a":1}'
Write output to file Get-Process | Out-File procs.txt
Output to file and pipeline Get-Process | Tee-Object -FilePath log.txt

Control Flow

Command Description Example
Conditional branching if ($x -gt 10) { 'big' } else { 'small' }
Switch statement switch ($day) { 'Mon' { 'Monday' } default { 'Other' } }
For loop for ($i=0; $i -lt 10; $i++) { $i }
Foreach loop foreach ($f in Get-ChildItem) { $f.Name }
While loop while ($true) { Start-Sleep 1 }
Error handling try { 1/0 } catch { Write-Error $_ }
Comparison operators if ($a -eq $b) { 'equal' }
Logical operators if ($a -gt 0 -and $b -lt 10) { ... }

Functions

Command Description Example
Define a function function Add($a, $b) { $a + $b }
Advanced parameter attributes param([Parameter(Mandatory)]$Name)
Return from function function Get-Double($n) { return $n * 2 }

System

Command Description Example
List running processes Get-Process | Sort CPU -Desc | Select -First 10
Terminate a process Stop-Process -Name notepad -Force
List services Get-Service | Where Status -eq Running
Manage services Restart-Service -Name nginx
HTTP request Invoke-WebRequest https://api.example.com | Select Content
Background jobs Start-Job { Get-ChildItem -Recurse } | Wait-Job
Get current date/time Get-Date -Format 'yyyy-MM-dd HH:mm:ss'
Output text Write-Host 'Hello' -ForegroundColor Green

Help

Command Description Example
Get help for a command Get-Help Get-Process -Examples
Find available commands Get-Command *service* → list matching cmds
List object properties/methods Get-Process | Get-Member → show properties

Frequently asked questions

What's the difference between PowerShell and CMD?

CMD is the legacy Windows command processor with a limited command set. PowerShell is a modern, object-oriented shell with .NET integration, a rich scripting language, pipeline processing, and cross-platform support. PowerShell replaces CMD for all but the simplest tasks.

Does PowerShell work on Mac and Linux?

Yes! PowerShell Core (7.x) is cross-platform and runs on Windows, macOS, and Linux. Install via brew (macOS), apt/snap (Linux), or download from GitHub. Most cmdlets work cross-platform, though some Windows-specific ones (like Get-Service) are Windows-only.

How do I run a PowerShell script?

Save as .ps1 file, then run with ./script.ps1. You may need to set execution policy first: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser. This allows running local scripts while blocking unsigned remote scripts.

What are the most useful PowerShell cmdlets?

Essential cmdlets: Get-Help (documentation), Get-Command (find commands), Get-Member (inspect objects), Get-ChildItem (list files), Where-Object (filter), ForEach-Object (transform), Select-Object (project), Sort-Object (sort), and Invoke-WebRequest (HTTP).

How do I handle errors in PowerShell?

Use try/catch/finally for structured error handling. Set $ErrorActionPreference = 'Stop' to make all errors catchable. In catch blocks, use $_ or $PSItem to access the error object. Use -ErrorAction Stop on individual cmdlets to override the default preference.

How does PowerShell compare to Bash?

Bash passes text through pipes; PowerShell passes objects. This means no parsing grep/awk/sed - just access properties. Bash is more concise for simple text tasks. PowerShell is more powerful for structured data, Windows admin, and complex scripting. Many devs use both.

Go from reference to real skills

Cheat sheets are great for quick lookups. Our in-depth courses take you from the fundamentals to professional-level mastery.

Browse all courses