PowerShell Cheat Sheet

January 16, 2025

  • Get-Location / pwd - Get current directory
  • Set-Location path / cd path - Change directory
  • cd ~ - Go to home directory
  • cd .. - Go up one directory
  • Get-ChildItem / ls / dir - List files
  • Get-ChildItem -Force - Show hidden files
  • Get-ChildItem -Recurse - List recursively

File Operations

  • Copy-Item file1 file2 / cp file1 file2 - Copy file
  • Copy-Item -Recurse dir1 dir2 - Copy directory
  • Move-Item file1 file2 / mv file1 file2 - Move/rename
  • Remove-Item file / rm file / del file - Delete file
  • Remove-Item -Recurse dir - Delete directory
  • New-Item -ItemType File file - Create file
  • New-Item -ItemType Directory dir - Create directory
  • Get-Content file / cat file - Read file contents
  • Set-Content file "text" - Write to file
  • Add-Content file "text" - Append to file

File Permissions

  • Get-Acl file - Get file permissions
  • Set-Acl file acl - Set file permissions
  • icacls file - Modify file permissions

Search & Find

  • Select-String pattern file - Search for pattern
  • Get-ChildItem -Recurse -Filter "*.txt" - Find files
  • Where-Object { $_.Property -eq value } - Filter objects
  • Select-Object Property1, Property2 - Select properties

Text Processing

  • Write-Host "text" / echo "text" - Print text
  • Get-Content file | Measure-Object -Line - Count lines
  • Get-Content file | Sort-Object - Sort lines
  • Get-Content file | Select-Object -Unique - Remove duplicates
  • $text -replace "old", "new" - Replace text
  • $text.Split("delimiter") - Split string

Process Management

  • Get-Process - List running processes
  • Get-Process -Name name - Find process by name
  • Stop-Process -Id PID - Stop process
  • Stop-Process -Name name - Stop process by name
  • Start-Process command - Start process
  • Get-Job - List background jobs
  • Start-Job script - Start background job
  • Wait-Job job - Wait for job to complete
  • Receive-Job job - Get job output

System Info

  • Get-PSDrive - List drives
  • Get-WmiObject Win32_LogicalDisk - Disk information
  • Get-ComputerInfo - System information
  • Get-Date - Current date/time
  • $env:COMPUTERNAME - Computer name
  • $env:USERNAME - Current user
  • Get-Host - PowerShell version

Variables & Objects

  • $var = value - Assign variable
  • $var - Access variable
  • Get-Variable - List all variables
  • Remove-Variable var - Delete variable
  • [string]$var - Type cast to string
  • [int]$var - Type cast to integer
  • $PSVersionTable - PowerShell version info

Pipes & Filtering

  • command1 | command2 - Pipe output
  • | Where-Object { condition } - Filter objects
  • | Select-Object Property - Select properties
  • | Sort-Object Property - Sort objects
  • | Group-Object Property - Group objects
  • | Format-Table - Format as table
  • | Format-List - Format as list

Modules & Help

  • Get-Module - List loaded modules
  • Import-Module module - Import module
  • Get-Command - List commands
  • Get-Help command - Show help
  • Get-Help command -Examples - Show examples
  • Get-Help command -Online - Open online help

Aliases

  • Get-Alias - List aliases
  • Set-Alias alias command - Create alias
  • alias - Show alias definition
  • Common aliases: ls, dir, cat, cd, pwd, echo, rm, cp, mv

Execution Policy

  • Get-ExecutionPolicy - Check execution policy
  • Set-ExecutionPolicy RemoteSigned - Allow local scripts
  • Set-ExecutionPolicy Bypass - Allow all scripts
  • powershell -ExecutionPolicy Bypass script.ps1 - Run with policy