PSPowerShell · Lesson 8 of 8

PowerShell Cheatsheet

Cmdlets, objects, and scripting syntax on one page.

PowerShell
# ── Files & navigation ──────────────────
Get-Location; Set-Location dir      # pwd / cd
Get-ChildItem -Recurse -Filter *.js # ls / find
Copy-Item src dst -Recurse
Move-Item old new; Remove-Item file
New-Item -ItemType Directory -Path a\b -Force
Get-Content file; Get-Content log -Tail 20 -Wait

# Aliases exist: ls, cd, cp, mv, rm, cat, pwd all work

# ── Objects, not text — the big idea ────
Get-Process | Where-Object CPU -gt 100 |
    Sort-Object CPU -Descending |
    Select-Object Name, CPU -First 5

Get-ChildItem | Measure-Object Length -Sum
(Get-Process).Count
Get-Process | Get-Member          # inspect properties

# ── Finding things ──────────────────────
Select-String -Pattern "TODO" -Path src\*.ts -Recurse
Get-Command *service*             # find cmdlets
Get-Help Get-Process -Examples
PowerShell
# ── Variables & types ───────────────────
$name = "Ada"
$n = 42
"$name is $n"                     # interpolation
$items = @(3, 1, 4)               # array
$ages = @{ Ada = 17; Bob = 15 }   # hashtable
$ages["Ada"]; $ages.Ada
[int]"42"; "$n".GetType()

# ── Control flow ────────────────────────
if ($n -gt 10) { } elseif ($n -gt 5) { } else { }
# operators: -eq -ne -gt -lt -ge -le -like -match -contains
foreach ($item in $items) { $item }
$items | ForEach-Object { $_ * 2 }
$items | Where-Object { $_ -gt 2 }
1..5                              # range
switch ($n) { 1 { "one" }; default { "many" } }

# ── Functions & scripts ─────────────────
function Get-Greeting {
    param(
        [Parameter(Mandatory)] [string]$Name,
        [int]$Times = 1
    )
    "Hello, $Name! " * $Times
}
Get-Greeting -Name Ada -Times 2

try { Risky } catch { Write-Error $_ } finally { Cleanup }

# ── Useful one-liners ───────────────────
Invoke-RestMethod https://api.github.com/users/octocat
Get-Process node | Stop-Process
Test-Path .\config.json
ConvertTo-Json $ages; ConvertFrom-Json $text
Get-Date -Format "yyyy-MM-dd"