Skip to content

Basics

Windows PowerShell PowerShell 7
Program powershell.exe pwsh.exe
Version 5.1 (frozen, ships with Windows) 7.x (current, cross-platform)
Install Already there winget install --id Microsoft.PowerShell -e

Everything in this section works in both unless noted. Check yours with:

Terminal window
$PSVersionTable.PSVersion

Commands follow a Verb-Noun pattern: Get-Process, Stop-Service, New-Item…

Terminal window
Get-Command *service* # find commands by name
Get-Help Get-ChildItem -Examples # usage examples
Get-Help Get-ChildItem -Online # open the docs in the browser
Get-Alias ls # what an alias really runs
Terminal window
Get-Process |
Where-Object CPU -gt 100 |
Sort-Object CPU -Descending |
Select-Object -First 5 Name, Id, CPU
  • Where-Object filters, Sort-Object sorts, Select-Object picks properties or the first N items.
  • Get-Member shows which properties and methods an object has: Get-Process | Get-Member.
  • A line ending in | continues on the next line. Elsewhere, the line-continuation character is the backtick `.

Output formats:

Terminal window
Get-Process | Format-Table Name, Id, CPU -AutoSize
Get-Process | Export-Csv processes.csv -NoTypeInformation
Get-Process | Select-Object Name, Id | ConvertTo-Json
Unix PowerShell Alias
ls Get-ChildItem ls, dir, gci
cd Set-Location cd
pwd Get-Location pwd
cat Get-Content cat, type
tail -f Get-Content -Tail 20 -Wait
grep Select-String sls
cp, mv, rm Copy-Item, Move-Item, Remove-Item cp, mv, rm
which Get-Command gcm
ps, kill Get-Process, Stop-Process ps, kill
df Get-PSDrive, Get-Volume
free Get-CimInstance Win32_OperatingSystem
env Get-ChildItem Env:

The aliases accept PowerShell parameters, not Unix ones: ls -la fails, ls -Force works.

Terminal window
$name = "dumdocentdiscunt"
$env:PATH -split ';' # the PATH, one folder per line
$env:MY_VAR = "value" # only for this session
$HOME # your user folder

If a .ps1 script refuses to run because of the execution policy:

Terminal window
Get-ExecutionPolicy -List
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

Your profile, a script that runs every time PowerShell starts, lives at $PROFILE. Create and open it with:

Terminal window
if (-not (Test-Path $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force }
notepad $PROFILE