Print me
PowerShell cheat sheet
Every command on the site, grouped by task. Print it, or choose Save as PDF in the print dialog.
Files
- Append text to a file
Add-Content -Path .\notes.txt -Value 'Another line'- Build a path safely
Join-Path -Path .\docs -ChildPath 'notes.txt'- Change the current directory
Set-Location -Path .\docs- Check a file's hash (checksum)
Get-FileHash -Path .\installer.exe -Algorithm SHA256- Check whether a path exists
Test-Path -Path .\docs- Copy a file or folder
Copy-Item -Path .\notes.txt -Destination .\backup\notes.txt- Create a file or folder
New-Item -Path .\docs -ItemType Directory- Delete a file or folder
Remove-Item -Path .\temp\old.log- Get one file, folder, or item
Get-Item -Path .\notes.txt- List files in a folder
Get-ChildItem -Path .\docs- Move or rename by moving
Move-Item -Path .\notes.txt -Destination .\archive\notes.txt- Overwrite a text file
Set-Content -Path .\notes.txt -Value 'Hello'- Pull a folder or name out of a path
Split-Path -Path .\docs\notes.txt -Parent- Read a text file
Get-Content -Path .\notes.txt- Read item properties
Get-ItemProperty -Path .\notes.txt- Rename a file or folder
Rename-Item -Path .\draft.txt -NewName final.txt- See who can access a file or folder
(Get-Acl -Path .\docs).Access | Select-Object IdentityReference, FileSystemRights, AccessControlType- Send output to a file
Get-ChildItem | Out-File -FilePath .\listing.txt- Show the current folder
Get-Location- Turn a relative path into a full path
Resolve-Path -Path .\docs- Unzip a file
Expand-Archive -Path .\docs.zip -DestinationPath .\docs-unzipped- Zip a folder or files
Compress-Archive -Path .\docs -DestinationPath .\docs.zip
Help and discovery
- Allow a downloaded script to run
Unblock-File -Path .\script.ps1- Allow scripts to run on this computer
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser- Find a cmdlet by name
Get-Command -Name *Item- Install a module from the PowerShell Gallery
Install-Module -Name ImportExcel -Scope CurrentUser- List command aliases
Get-Alias -Name gci- Read help for a command
Get-Help Get-ChildItem -Full- Search the PowerShell Gallery
Find-Module -Tag Excel | Select-Object -First 10 Name, Version, Description- See commands you ran earlier
Get-History -Count 20- See loaded and available modules
Get-Module -ListAvailable- See properties and methods on an object
Get-ChildItem | Get-Member- See whether scripts are allowed to run
Get-ExecutionPolicy -List
Network
- Call a JSON API
Invoke-RestMethod -Uri 'https://example.com'- Fetch a web page or file
Invoke-WebRequest -Uri 'https://example.com' -UseBasicParsing- Find which program is using a port
Get-NetTCPConnection -LocalPort 8080 | Select-Object LocalAddress, LocalPort, State, OwningProcess- List local IP addresses
Get-NetIPAddress -AddressFamily IPv4 | Select-Object InterfaceAlias, IPAddress- Look up a DNS name
Resolve-DnsName -Name example.com- Ping a host
Test-Connection -ComputerName 1.1.1.1 -Count 2- Test a TCP port or route
Test-NetConnection -ComputerName example.com -Port 443
System
- Check free disk space
Get-Volume | Select-Object DriveLetter, FileSystemLabel, SizeRemaining, Size- Clear the screen
Clear-Host- Get the current date and time
Get-Date -Format 'yyyy-MM-dd HH:mm:ss'- List drives PowerShell can see
Get-PSDrive- List local user accounts
Get-LocalUser | Select-Object Name, Enabled, LastLogon- List running processes
Get-Process | Sort-Object CPU -Descending | Select-Object -First 10- List Windows services
Get-Service | Where-Object Status -eq 'Running'- Open a file or start a program
Start-Process -FilePath .\notes.txt- Pause for N seconds
Start-Sleep -Seconds 5- Query system details (WMI/CIM)
Get-CimInstance -ClassName Win32_OperatingSystem | Select-Object Caption, Version, LastBootUpTime- Read the Windows event log
Get-WinEvent -LogName System -MaxEvents 20- Record the session to a log file
Start-Transcript -Path (Join-Path $env:TEMP 'ps-transcript.txt')- Restart a Windows service
Restart-Service -Name Spooler -WhatIf- Restart the computer
Restart-Computer -WhatIf- Run a command on another computer
Invoke-Command -ComputerName PC01 -ScriptBlock { Get-Service -Name Spooler }- Start a Windows service
Start-Service -Name Spooler- Stop a process
Stop-Process -Name notepad -WhatIf- Stop a Windows service
Stop-Service -Name Spooler -WhatIf- Summarize this computer
Get-ComputerInfo | Select-Object CsName, WindowsVersion, OsArchitecture- Time how long a command takes
Measure-Command { Get-ChildItem -Path .\docs -Recurse }
Text and objects
- Browse and filter results in a window
Get-Process | Out-GridView- Compare two lists
Compare-Object -ReferenceObject $old -DifferenceObject $new- Copy output to the clipboard
Get-ChildItem -Name | Set-Clipboard- Count, sum, or average
Get-ChildItem -File | Measure-Object -Property Length -Sum- Filter objects in the pipeline
Get-ChildItem | Where-Object { $_.Extension -eq '.md' }- Group objects by a property
Get-ChildItem -File | Group-Object -Property Extension- Parse JSON text
Get-Content -Path .\data.json -Raw | ConvertFrom-Json- Pick properties or the first N objects
Get-ChildItem | Select-Object -First 5 Name, Length- Print a message (Write-Output vs Write-Host)
Write-Output 'Backup finished'- Read a CSV file as objects
Import-Csv -Path .\people.csv- Read what is on the clipboard
Get-Clipboard- Run a block on each object
Get-ChildItem -File | ForEach-Object { $_.Name }- Search text like grep
Select-String -Path .\docs\*.md -Pattern 'TODO'- Show objects as a list
Get-Item -Path .\notes.txt | Format-List *- Show objects as a table
Get-ChildItem | Format-Table Name, Length -AutoSize- Sort pipeline objects
Get-ChildItem | Sort-Object -Property Length -Descending- Turn objects into JSON
Get-ChildItem -File | Select-Object Name, Length | ConvertTo-Json- Turn results into an HTML report
Get-Service | ConvertTo-Html -Property Name, Status -Title 'Services' | Out-File -FilePath .\services.html- Write objects to a CSV file
Get-ChildItem -File | Select-Object Name, Length | Export-Csv -Path .\files.csv -NoTypeInformation
Stay safe
- Preview a change
-WhatIf- Ask before each change
-Confirm- Help for any command
Get-Help Get-ChildItem -Examples- What is in this object?
Get-ChildItem | Get-Member
More commands, scripts, and builders: https://tmhsdigital.github.io/copy-paste-powershell/