Skip to content
Commitpedia
Log in Join & edit

PowerShell cheat sheet

The PowerShell cmdlets and patterns you use every day: files, text, processes, services, filtering, exporting, remoting and the operators that trip up people coming from bash.

Drafted with AI from official documentation. Scripts were run in a test environment and repository links verified. Awaiting a human check. Did it work for you? Last edited Outdated?

Edit history of PowerShell cheat sheet: 1 recorded edit
Edit history · 1 edit
Type
Article
Edits
1
Editors
1
First issued
15 Oct 2026
Last edit
15 Oct 2026
Reading
3 min

A single page to keep open while you work. Everything below runs in PowerShell 7 (pwsh) and, unless marked, in Windows PowerShell 5.1 as well. Lines starting with # are comments.

Help and discovery

Get-Command *network*                 # find cmdlets by name
Get-Command -Module Microsoft.PowerShell.Management
Get-Help Copy-Item -Examples          # examples for a cmdlet
Get-Help about_Operators              # conceptual help topics
Get-Alias                             # all aliases
Get-Alias -Definition Get-ChildItem   # aliases for one cmdlet
$PSVersionTable                       # which PowerShell is this?

Files and folders

Get-Location                                  # pwd
Set-Location C:\Projects                      # cd
Get-ChildItem                                 # ls
Get-ChildItem -Recurse -Filter *.log          # find by name
Get-ChildItem -Force                          # include hidden files
New-Item -ItemType Directory -Path .\build    # mkdir
New-Item -ItemType File -Path .\notes.txt     # touch
Copy-Item .\a.txt .\b.txt
Copy-Item .\src -Destination .\backup -Recurse
Move-Item .\old.txt .\new.txt                 # move or rename
Rename-Item .\report.txt report-final.txt
Remove-Item .\build -Recurse                  # rm -r
Test-Path .\config.json                       # does it exist? True/False
Get-Item .\a.txt | Select-Object Length, LastWriteTime
Resolve-Path .\..\other                        # absolute path

Reading and writing text

Get-Content .\app.log                         # cat
Get-Content .\app.log -Tail 20                # last 20 lines
Get-Content .\app.log -Wait                   # tail -f
Get-Content .\app.log | Select-String error   # grep (case-insensitive by default)
Select-String -Path *.log -Pattern 'timeout' -CaseSensitive
'hello' | Out-File .\out.txt                  # write (overwrite)
'more'  | Add-Content .\out.txt               # append
Set-Content .\out.txt -Value 'replace all'
(Get-Content .\a.txt) -replace 'foo', 'bar' | Set-Content .\a.txt

Filtering, sorting, shaping

Get-Process | Where-Object { $_.WorkingSet64 -gt 500MB }
Get-Process | Where-Object Name -like 'chrome*'       # simplified syntax
Get-Service | Sort-Object Status, Name
Get-ChildItem | Sort-Object Length -Descending | Select-Object -First 10 Name, Length
Get-Process | Select-Object Name, @{Name='MB'; Expression={ [math]::Round($_.WorkingSet64 / 1MB) }}
Get-ChildItem | Measure-Object -Property Length -Sum
Get-Service | Group-Object Status
Get-ChildItem | ForEach-Object { $_.Name.ToUpper() }

Processes, services, system

Get-Process
Get-Process -Name notepad
Stop-Process -Name notepad                # careful: ends the program
Start-Process notepad.exe
Start-Process pwsh -Verb RunAs            # new elevated window (Windows)
Get-Service -Name Spooler
Restart-Service -Name Spooler             # Windows, administrator
Get-ComputerInfo | Select-Object OsName, OsVersion, CsName     # Windows
Get-NetIPAddress -AddressFamily IPv4      # Windows
Test-NetConnection example.com -Port 443  # Windows
Get-Uptime                                # PowerShell 6+

Export and import data

Get-Service | Export-Csv services.csv -NoTypeInformation
Import-Csv services.csv | Where-Object Status -eq 'Running'
Get-Process | Select-Object Name, Id | ConvertTo-Json | Set-Content procs.json
Get-Content procs.json | ConvertFrom-Json
Invoke-RestMethod https://api.github.com/repos/PowerShell/PowerShell | Select-Object stargazers_count
Invoke-WebRequest https://example.com -OutFile page.html

Variables, arrays, hashtables

$x = 5
$list = @('a', 'b', 'c');  $list[0];  $list.Count
$list += 'd'
$map = @{ Name = 'Ana'; Role = 'admin' };  $map.Name;  $map['Role']
$map.Keys
$obj = [pscustomobject]@{ Host = 'web1'; Up = $true }
$env:PATH                               # environment variables live in $env:
$env:MY_SETTING = 'value'               # for this session only
$HOME; $PSScriptRoot; $PWD; $?; $LASTEXITCODE

Operators that differ from bash

  • Comparison: -eq -ne -gt -ge -lt -le. Add c for case sensitive (-ceq).
  • Strings: -like 'a*' (wildcards), -match '^a.*' (regex, sets $Matches), -replace, -split, -join.
  • Collections: -contains, -in, -notcontains.
  • Logic: -and -or -not (or !).
  • && and || exist only in PowerShell 7. In 5.1 use ; and check $?.
  • Call a program whose path is in a string or has spaces with &: & 'C:\Program Files\App\app.exe' --flag.
  • Backtick ` is the escape and line-continuation character, not backslash.

Control flow

if ($x -gt 3) { 'big' } elseif ($x -eq 3) { 'three' } else { 'small' }
foreach ($f in Get-ChildItem *.txt) { $f.Name }
for ($i = 0; $i -lt 3; $i++) { $i }
while ($true) { break }
switch ($x) { 1 { 'one' } 5 { 'five' } default { 'other' } }
try { Get-Item missing.txt -ErrorAction Stop } catch { "Failed: $($_.Exception.Message)" } finally { 'done' }
function Get-Greeting { param([string]$Name = 'world') "Hello, $Name" }
Get-Greeting -Name Ana

Scripts

.\script.ps1                                  # run a script in the current folder
pwsh -NoProfile -File .\script.ps1 -Verbose    # run from cmd or a scheduler
Get-ExecutionPolicy -List                      # why won't scripts run?
$PROFILE                                       # path of your startup script
notepad $PROFILE                               # edit it (created if missing)

Remote machines (Windows)

Enter-PSSession -ComputerName server01               # interactive session
Invoke-Command -ComputerName server01, server02 -ScriptBlock { Get-Service W32Time }
Invoke-Command -ComputerName server01 -FilePath .\audit.ps1
ssh user@linuxhost                                   # OpenSSH client is built in

Remoting over WinRM has to be enabled on the target first (Enable-PSRemoting, run as administrator). Careful: that opens a listener and firewall rule; Disable-PSRemoting reverses it, and the documentation lists the extra cleanup steps.

Official documentation

Did this work for you?

No votes yet. Be the first.

If it did not work, the report form below tells the editors what went wrong.

See also

  • 0396 PowerShell basics: cmdlets, objects and the pipeline PowerShell commands are called cmdlets, they return objects instead of text, and the pipeline passes those objects between them. Once that clicks,… Beginner · Explains an idea · Linux, macOS, Windows Article PowerShell 15 Oct 2026
  • 0399 Fix: running scripts is disabled on this system (execution policy) PowerShell refuses to run .ps1 files until the execution policy allows it. The usual fix is RemoteSigned for your own user; here… Beginner · Fixes an error · Windows Article PowerShell · Windows administration 15 Oct 2026
  • 0401 Windows services, Task Scheduler and event logs from PowerShell Manage the three things Windows administrators touch most: query and restart services, create and inspect scheduled tasks, and search the event logs… Intermediate · Step by step · Windows Article PowerShell · Windows administration 15 Oct 2026
  • 0404 Find the largest folders on a drive with PowerShell Lists the biggest top-level folders under a path, with file counts and sizes in GB, so you can see where the disk… Beginner · Step by step · Linux, macOS, Windows Script PowerShell · Windows administration 15 Oct 2026
  • 0405 Export the list of installed programs to CSV with PowerShell Reads the Windows uninstall registry keys and writes name, version, publisher and install date of every installed program to a CSV file.… Beginner · Step by step · Windows Script PowerShell · Windows administration 15 Oct 2026

Repos in the wild

History

Every saved edit is kept. The plot on the sleeve draws one line per edit. Open any edit to see exactly what changed.

  1. Dominik +6,388 chars

Report a problem

Something wrong, unsafe or copied? Tell the editors.
What is wrong?