← All cheat sheets · 🐧 Linux shell
🪟 Windows shell
PowerShell for everyday Windows work, with the classic Command Prompt (cmd) form where people still meet it.
72 commands in 12 sections
Move around and manage files
Get-LocationPrint the directory you are in
also
cdGet-ChildItem -ForceList files, including hidden ones
also
dir /als and dir also work in PowerShell.
Set-Location C:\projectsChange directory
also
cd /d D:\projectsNew-Item -ItemType Directory -Force a\b\cCreate a directory and any missing parents
also
mkdir a\b\cCopy-Item src dest -RecurseCopy a file or a whole folder
also
xcopy src dest /E /Irobocopy src dest /MIRMirror a folder, copying only what changed
/MIR also deletes files in dest that are not in src.
Move-Item old newMove or rename
also
move old newRemove-Item .\build -Recurse -ForceDelete a folder and everything in it
also
rmdir /s /q buildThere is no recycle bin from the shell.
New-Item notes.txtCreate an empty file
also
type nul > notes.txt
Read text
Get-Content file.txtPrint a file
also
type file.txtGet-Content app.log -Tail 20 -WaitFollow the end of a log as it grows
Get-Content app.log -TotalCount 20Show the first 20 lines
(Get-Content app.log | Measure-Object -Line).LinesCount lines
Compare-Object (Get-Content a.txt) (Get-Content b.txt)Show lines that differ between two files
also
fc a.txt b.txtnotepad file.txtOpen a file in Notepad
Search
Select-String -Path *.log -Pattern 'error'Find text in files, with file names and line numbers
also
findstr /s /i /n "error" *.logGet-ChildItem -Recurse -Filter *.logFind files by name under a folder
also
dir /s /b *.logGet-ChildItem -Recurse -File | Where-Object Length -gt 100MBFind files bigger than 100 MB
Get-Command gitShow which program a command runs
also
where.exe gitGet-History | Select-String sshSearch the commands you ran in this session
Ctrl+R searches history as you type.
Work with objects in the pipeline
Where-ObjectKeep only the items that match
e.g.
Get-Service | Where-Object Status -eq 'Running'Select-ObjectPick properties, or the first few items
e.g.
Get-Process | Select-Object -First 5 Name, CPUSort-ObjectSort items by a property
e.g.
Get-Process | Sort-Object CPU -DescendingGroup-ObjectCount items by a property
e.g.
Get-ChildItem | Group-Object ExtensionForEach-ObjectRun something for each item
e.g.
1..3 | ForEach-Object { "Item $_" }Get-Content data.json -Raw | ConvertFrom-JsonRead JSON into objects
also
ConvertTo-JsonExport-Csv out.csv -NoTypeInformationSave objects as CSV
e.g.
Get-Process | Export-Csv procs.csv -NoTypeInformationGet-MemberList an object's properties and methods
e.g.
Get-Item . | Get-Member
Processes
Get-ProcessList running processes
also
tasklistStop-Process -Id 1234 -ForceStop a process by id
also
taskkill /PID 1234 /FStop-Process -Name notepadStop processes by name
also
taskkill /IM notepad.exeStart-Process pwsh -Verb RunAsOpen an administrator PowerShell window
pwsh is PowerShell 7; use powershell for the built-in Windows PowerShell 5.1.
Start-Job { Start-Sleep 60 }Run a command in the background
also
Get-JobReceive-Job collects its output.
Disks and system
Get-VolumeFree space on each drive
also
Get-PSDrive -PSProvider FileSystemGet-ChildItem -Recurse -File | Measure-Object Length -SumTotal size of a folder in bytes
Get-ComputerInfoWindows version, hardware and more
also
systeminfoTakes a few seconds.
Get-CimInstance Win32_OperatingSystemVersion, memory and last boot time
$PSVersionTableWhich PowerShell version this is
Network
Test-NetConnection example.com -Port 443Check a host is reachable on a port
also
tnc example.com -Port 443Get-NetTCPConnection -State ListenWhich ports are listening
also
netstat -anoGet-NetTCPConnection -LocalPort 8080Which process is using port 8080
OwningProcess is the process id; look it up with Get-Process -Id.
Resolve-DnsName example.comLook up a DNS record
also
nslookup example.comipconfig /allNetwork interfaces and IP addresses
also
Get-NetIPAddressipconfig /flushdnsClear the DNS cache
Invoke-RestMethod https://api.example.com/itemsCall an API and get objects back
also
Invoke-WebRequestcurl.exe -I https://example.comUse real curl (built into Windows 10 and later)
In Windows PowerShell 5.1, plain curl is an alias for Invoke-WebRequest.
ssh user@hostOpen a shell on another machine (OpenSSH is built in)
also
scp file user@host:/path/
Archives
Compress-Archive -Path .\site\* -DestinationPath site.zipPack a folder into a .zip
Expand-Archive site.zip -DestinationPath .\siteUnpack a .zip
tar -xf archive.tar.gzUnpack a .tar.gz (tar is built into Windows 10 and later)
also
tar -czf site.tar.gz site
Packages, services and tasks
winget search gitFind an app to install
winget install Git.GitInstall an app
winget upgrade --allUpdate every app winget knows about
Get-ServiceList services and whether they run
also
sc.exe queryRestart-Service SpoolerRestart a service (needs an administrator shell)
also
net stop spooler && net start spoolerSet-Service Spooler -StartupType AutomaticStart a service on every boot
Get-ScheduledTaskList scheduled tasks
also
schtasks /query
Environment, profile and help
$env:PATH -split ";"Show each folder on the PATH
also
echo %PATH%$env:NAME = "value"Set an environment variable for this window
also
set NAME=value[Environment]::SetEnvironmentVariable("NAME", "value", "User")Set a variable permanently for your user
also
setx NAME "value"Open a new window to see it.
notepad $PROFILEEdit the script that runs when PowerShell starts
Put aliases and functions there to keep them.
Set-ExecutionPolicy -Scope CurrentUser RemoteSignedAllow your own scripts to run
Get-Help Get-Process -ExamplesExamples for any command
also
Get-Command *service*Get-AliasList short names, such as ls for Get-ChildItem
Permissions and users
whoamiPrint your user name
also
whoami /groupsGet-Acl .\secrets.txt | Format-ListWho may do what with a file
also
icacls .\secrets.txtUnblock-File .\script.ps1Trust a downloaded script so it can run
Shell tricks
command > out.txtSend output to a file, replacing it
also
command >> out.txtcommand *>&1 | Tee-Object run.logShow all output, including errors, and save it to a file
$(command)Use a command's output inside a string
e.g.
"Today is $(Get-Date -Format yyyy-MM-dd)"Get-ClipboardPaste the clipboard into the pipeline
e.g.
Get-Content file.txt | Set-Clipboardalso
Set-ClipboardClear-HostClear the screen
also
cls