TwiceToldby Develoker
ConceptsSystem designLabsCheat sheetsAbout

Develoker makes free tools for learning to build software. TwiceTold explains each idea twice, as an everyday analogy and the real thing; CloudArch lets you design cloud systems and put load on them.

About · Labs · Cheat sheets · Learning paths

← 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 filesRead textSearchWork with objects in the pipelineProcessesDisks and systemNetworkArchivesPackages, services and tasksEnvironment, profile and helpPermissions and usersShell tricks

Move around and manage files

  • Get-Location

    Print the directory you are in

    also cd

  • Get-ChildItem -Force

    List files, including hidden ones

    also dir /a

    ls and dir also work in PowerShell.

  • Set-Location C:\projects

    Change directory

    also cd /d D:\projects

  • New-Item -ItemType Directory -Force a\b\c

    Create a directory and any missing parents

    also mkdir a\b\c

  • Copy-Item src dest -Recurse

    Copy a file or a whole folder

    also xcopy src dest /E /I

  • robocopy src dest /MIR

    Mirror a folder, copying only what changed

    /MIR also deletes files in dest that are not in src.

  • Move-Item old new

    Move or rename

    also move old new

  • Remove-Item .\build -Recurse -Force

    Delete a folder and everything in it

    also rmdir /s /q build

    There is no recycle bin from the shell.

  • New-Item notes.txt

    Create an empty file

    also type nul > notes.txt

Read text

  • Get-Content file.txt

    Print a file

    also type file.txt

  • Get-Content app.log -Tail 20 -Wait

    Follow the end of a log as it grows

  • Get-Content app.log -TotalCount 20

    Show the first 20 lines

  • (Get-Content app.log | Measure-Object -Line).Lines

    Count lines

  • Compare-Object (Get-Content a.txt) (Get-Content b.txt)

    Show lines that differ between two files

    also fc a.txt b.txt

  • notepad file.txt

    Open 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" *.log

  • Get-ChildItem -Recurse -Filter *.log

    Find files by name under a folder

    also dir /s /b *.log

  • Get-ChildItem -Recurse -File | Where-Object Length -gt 100MB

    Find files bigger than 100 MB

  • Get-Command git

    Show which program a command runs

    also where.exe git

  • Get-History | Select-String ssh

    Search the commands you ran in this session

    Ctrl+R searches history as you type.

Work with objects in the pipeline

  • Where-Object

    Keep only the items that match

    e.g. Get-Service | Where-Object Status -eq 'Running'

  • Select-Object

    Pick properties, or the first few items

    e.g. Get-Process | Select-Object -First 5 Name, CPU

  • Sort-Object

    Sort items by a property

    e.g. Get-Process | Sort-Object CPU -Descending

  • Group-Object

    Count items by a property

    e.g. Get-ChildItem | Group-Object Extension

  • ForEach-Object

    Run something for each item

    e.g. 1..3 | ForEach-Object { "Item $_" }

  • Get-Content data.json -Raw | ConvertFrom-Json

    Read JSON into objects

    also ConvertTo-Json

  • Export-Csv out.csv -NoTypeInformation

    Save objects as CSV

    e.g. Get-Process | Export-Csv procs.csv -NoTypeInformation

  • Get-Member

    List an object's properties and methods

    e.g. Get-Item . | Get-Member

Processes

  • Get-Process

    List running processes

    also tasklist

  • Stop-Process -Id 1234 -Force

    Stop a process by id

    also taskkill /PID 1234 /F

  • Stop-Process -Name notepad

    Stop processes by name

    also taskkill /IM notepad.exe

  • Start-Process pwsh -Verb RunAs

    Open 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-Job

    Receive-Job collects its output.

Disks and system

  • Get-Volume

    Free space on each drive

    also Get-PSDrive -PSProvider FileSystem

  • Get-ChildItem -Recurse -File | Measure-Object Length -Sum

    Total size of a folder in bytes

  • Get-ComputerInfo

    Windows version, hardware and more

    also systeminfo

    Takes a few seconds.

  • Get-CimInstance Win32_OperatingSystem

    Version, memory and last boot time

  • $PSVersionTable

    Which PowerShell version this is

Network

  • Test-NetConnection example.com -Port 443

    Check a host is reachable on a port

    also tnc example.com -Port 443

  • Get-NetTCPConnection -State Listen

    Which ports are listening

    also netstat -ano

  • Get-NetTCPConnection -LocalPort 8080

    Which process is using port 8080

    OwningProcess is the process id; look it up with Get-Process -Id.

  • Resolve-DnsName example.com

    Look up a DNS record

    also nslookup example.com

  • ipconfig /all

    Network interfaces and IP addresses

    also Get-NetIPAddress

  • ipconfig /flushdns

    Clear the DNS cache

  • Invoke-RestMethod https://api.example.com/items

    Call an API and get objects back

    also Invoke-WebRequest

  • curl.exe -I https://example.com

    Use real curl (built into Windows 10 and later)

    In Windows PowerShell 5.1, plain curl is an alias for Invoke-WebRequest.

  • ssh user@host

    Open a shell on another machine (OpenSSH is built in)

    also scp file user@host:/path/

Archives

  • Compress-Archive -Path .\site\* -DestinationPath site.zip

    Pack a folder into a .zip

  • Expand-Archive site.zip -DestinationPath .\site

    Unpack a .zip

  • tar -xf archive.tar.gz

    Unpack a .tar.gz (tar is built into Windows 10 and later)

    also tar -czf site.tar.gz site

Packages, services and tasks

  • winget search git

    Find an app to install

  • winget install Git.Git

    Install an app

  • winget upgrade --all

    Update every app winget knows about

  • Get-Service

    List services and whether they run

    also sc.exe query

  • Restart-Service Spooler

    Restart a service (needs an administrator shell)

    also net stop spooler && net start spooler

  • Set-Service Spooler -StartupType Automatic

    Start a service on every boot

  • Get-ScheduledTask

    List 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 $PROFILE

    Edit the script that runs when PowerShell starts

    Put aliases and functions there to keep them.

  • Set-ExecutionPolicy -Scope CurrentUser RemoteSigned

    Allow your own scripts to run

  • Get-Help Get-Process -Examples

    Examples for any command

    also Get-Command *service*

  • Get-Alias

    List short names, such as ls for Get-ChildItem

Permissions and users

  • whoami

    Print your user name

    also whoami /groups

  • Get-Acl .\secrets.txt | Format-List

    Who may do what with a file

    also icacls .\secrets.txt

  • Unblock-File .\script.ps1

    Trust a downloaded script so it can run

Shell tricks

  • command > out.txt

    Send output to a file, replacing it

    also command >> out.txt

  • command *>&1 | Tee-Object run.log

    Show 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-Clipboard

    Paste the clipboard into the pipeline

    e.g. Get-Content file.txt | Set-Clipboard

    also Set-Clipboard

  • Clear-Host

    Clear the screen

    also cls