PowerShell
A cross-platform shell and scripting language for automating systems, services, and developer workflows.
Why it exists.
It provides structured automation for Windows and cross-platform environments.
Where it shines.
Windows administration, Azure automation, and scripts that benefit from object-based pipelines.
Where not to use it.
Do not hide important operational behavior in opaque scripts without tests and documentation.
What it is made of.
- Cmdlets
- Objects
- Pipelines
- Modules
- Remoting
How it works.
- A command produces objects
- The pipeline transforms or filters them
- Modules provide reusable behavior
- The script reports the operation result
How it works
Run your first script
PowerShell executes cmdlets, functions, and external programs. Cmdlets usually produce objects. Save as hello.ps1 and run pwsh -File ./hello.ps1 with PowerShell 7 installed. Windows execution policy may also apply.
$name = "World"
Write-Output "Hello, $name!"Pipeline objects
The pipeline passes objects between cmdlets. Where-Object filters and Select-Object selects properties; $_ refers to the current pipeline object.
Get-Process |
Where-Object { $_.CPU -gt 10 } |
Select-Object ProcessName, CPUVariables & print
Assign and expand
Variables start with $. Double-quoted strings expand variables; single-quoted strings are literal. Arrays use @(...), and hashtables use @{...}.
$name = "Ada"
$count = 3
$enabled = $true
Write-Output "$name has $count tasks"
Write-Output 'Literal: $name'Output versus display
Write-Output emits pipeline data; Write-Host displays a message through the information stream. Use Read-Host for interactive input; param defines script parameters.
param([string]$Name = "World")
Write-Output "Hello, $Name"
Write-Host "Starting tutorial"
$environment = Read-Host "Environment"
Write-Output "Selected: $environment"If / else
Choose a branch
Use if, elseif, and else with braces. -gt means greater than, -eq means equal, and -ne means not equal.
$count = 3
if ($count -gt 5) {
Write-Output "Large batch"
} elseif ($count -gt 0) {
Write-Output "Small batch"
} else {
Write-Output "Nothing to process"
}Combine tests
Use -and, -or, and -not. String comparisons are case-insensitive by default; -ceq uses case-sensitive equality. Test-Path checks a file or directory.
$environment = "production"
$approved = $true
if (($environment -eq "production") -and $approved) {
Write-Output "Ready to deploy"
}
if (Test-Path -LiteralPath "config.json" -PathType Leaf) {
Write-Output "Config exists"
}For loops
Count with for
for has initialization, condition, and increment clauses. break exits a loop; continue advances to the next iteration.
for ($i = 1; $i -le 3; $i++) {
Write-Output "Attempt $i"
}Iterate with foreach
foreach enumerates a collection. ForEach-Object processes incoming pipeline objects. These are two useful ways to repeat work.
$environments = @("development", "staging", "production")
foreach ($environment in $environments) {
Write-Output "Environment: $environment"
}
$environments | ForEach-Object {
Write-Output "Selected: $_"
}API calls
GET and read JSON
Invoke-RestMethod converts JSON responses into objects. -TimeoutSec bounds the request. -ErrorAction Stop makes errors catchable by try/catch.
$headers = @{
Accept = "application/vnd.github+json"
"User-Agent" = "powershell-reference"
}
try {
$repo = Invoke-RestMethod -Uri "https://api.github.com/repos/octocat/Hello-World" -Headers $headers -TimeoutSec 20 -ErrorAction Stop
Write-Output $repo.full_name
} catch {
Write-Error "API request failed: $($_.Exception.Message)"
}POST JSON with authentication
Replace the example endpoint and supply API_TOKEN as an environment variable. ConvertTo-Json serializes the body. Avoid displaying credentials.
if (-not $env:API_TOKEN) { throw "Set API_TOKEN first" }
$headers = @{ Authorization = "Bearer $env:API_TOKEN" }
$body = @{ name = "demo" } | ConvertTo-Json
try {
$result = Invoke-RestMethod -Uri "https://api.example.com/items" -Method Post -Headers $headers -ContentType "application/json" -Body $body -TimeoutSec 20 -ErrorAction Stop
Write-Output $result
} catch {
Write-Error "API request failed: $($_.Exception.Message)"
}