PowerShell Cheat Sheet
Learn PowerShell from the basics →
Discovering Commands
Get-Command *service*- Find commands by name
Get-Help Get-Process -Examples- Help with examples
Get-Process | Get-Member- See an object's properties and methods
Get-Alias ls- What does an alias run?
Variables & Types
$name = "Ada"- Assign a variable
[int]$count = 5- Typed variable
"Hello, $name!" / 'literal $name'- Expanding vs literal strings
"Total: $($a + $b)"- Expression inside a string
$null / $true / $false- Special values
The Pipeline
Get-Process | Where-Object CPU -gt 100- Filter objects
| Sort-Object WorkingSet -Descending- Sort by a property
| Select-Object -First 5 Name, Id- Pick objects and properties
| ForEach-Object { $_.Name.ToUpper() }- Run code per object ($_ is the current one)
| Group-Object Status / | Measure-Object -Sum- Group / summarize
Operators
-eq -ne -gt -lt -ge -le- Comparison
-like "*.log" / -match "^\d+$"- Wildcard / regex match
-and -or -not- Logic
-contains / -in- Collection membership
$a ?? "default" / $x ? "yes" : "no"- Null-coalescing / ternary (PowerShell 7)
Collections
$list = @(1, 2, 3); $list += 4- Array
$user = @{ Name = "Ada"; Age = 36 }- Hash table
$user.Name / $user["Age"]- Read a key
[pscustomobject]@{ Name = "Ada"; Age = 36 }- Custom object
1..10- Range
Control Flow
if ($x -gt 0) { } elseif ($x -lt 0) { } else { }- If statement
switch ($day) { "Sat" { } default { } }- Switch
foreach ($f in $files) { }- Loop over a collection
for ($i = 0; $i -lt 5; $i++) { }- Counting loop
try { } catch { $_.Exception.Message } finally { }- Error handling
Functions
function Get-Greeting { param([string]$Name = "World") "Hello, $Name" }- Function with a parameter
[CmdletBinding()] param([Parameter(Mandatory)][string]$Path)- Advanced function parameters
[ValidateSet("Dev","Prod")]- Restrict allowed values
Write-Verbose / Write-Warning / Write-Error- Output streams
Files & Data
Get-ChildItem -Recurse -Filter *.log- Find files
Get-Content file.txt / Set-Content / Add-Content- Read / write / append text
Import-Csv data.csv / Export-Csv out.csv- CSV to objects and back
ConvertFrom-Json / ConvertTo-Json -Depth 5- JSON
Invoke-RestMethod https://api.example.com/items- Call a REST API