PowerShell Master Cheatsheet

A complete reference of common PowerShell commands, operators, scripting practices, and real-world usage examples. Optimized for quick lookup.


📌 Table of Contents


Getting Help

Get-Help                   # List all help topics
Get-Help Get-Command       # Help for a specific cmdlet
Get-Help Get-Command -Online  # Open online docs
Update-Help                # Download updated help content

Operators

$a = 2; $a += 1; $a -= 1    # Assignment and arithmetic
$a -eq 0; $a -ne 5          # Equality / inequality
$a -gt 2; $a -lt 3          # Greater / less than

# String comparison
"Trevor" -like 'T*'         # Wildcard match
"Celery" -in @("Bacon","Steak")
"Celery" -notin @("Bacon","Steak")

# Type checks
5 -is [int]; 5 -isnot [string]

Regular Expressions

"Trevor" -match '^T\w*'   # Match regex
$matches[0]                # Access match result

@("Trevor","Billy") -match '^B'  # Array regex filter

$regex = [regex]'(\w{3,8})'
$regex.Matches("Trevor Bobby").Value

Flow Control

if ($x -eq 1) { "Yes" }

while ($true) { break }
do { "Run Once" } while ($false)

for ($i=0; $i -lt 5; $i++) { Write-Output $i }

foreach ($p in Get-Process) { $p.Name }

switch -regex "Trevor" {
  '^T' { "Starts with T" }
}

Variables

$a = 0
[string]$name = "Zishan"

Get-Variable
New-Variable -Name FirstName -Value Trevor -Option Constant
Remove-Variable -Name FirstName -Force

Functions

function Add($a,$b) { $a + $b }

function Do-Something {
  [CmdletBinding()]
  param([string]$Input)
  process { "Processing $Input" }
}

Modules

Get-Module -ListAvailable
Import-Module PSReadLine
Remove-Module PSReadLine

New-ModuleManifest -Path ./MyModule.psd1

Module Management

Find-Module -Name PSScriptAnalyzer
Install-Module -Name PSScriptAnalyzer -Scope CurrentUser -Force
Uninstall-Module PSScriptAnalyzer

Filesystem

New-Item C:\Test -ItemType Directory
New-Item test.txt -ItemType File
Set-Content test.txt "Hello"
Get-Content test.txt
Remove-Item test.txt

Hashtables

$Person = @{ FirstName="Trevor"; Likes=@("Bacon","Software") }
$Person.FirstName
$Person.Age = 30

WMI / CIM

Get-CimInstance Win32_BIOS
Get-CimInstance Win32_NetworkAdapter
Get-CimClass -Namespace root\cimv2

Events

$Watcher = [System.IO.FileSystemWatcher]::new('C:\Temp')
Register-ObjectEvent -InputObject $Watcher -EventName Created -Action {
  Write-Host "File Created!"
}

$Timer = [System.Timers.Timer]::new(5000)
Register-ObjectEvent $Timer Elapsed -Action { "Timer tick" }
$Timer.Start()

PSDrives

Get-PSDrive
New-PSDrive -Name logs -PSProvider FileSystem -Root C:\Logs
Set-Location logs:

Data Management

Get-Process | Sort-Object -Property Name
Get-Process | Where-Object { $_.Name -match '^c' }
Get-Process | Group-Object -Property Name

Classes

class Person {
  [string]$FirstName
  [string]$LastName = "Sullivan"
  [int]$Age

  [string] FullName() {
    return "$($this.FirstName) $($this.LastName)"
  }
}

$p = [Person]::new()
$p.FirstName = "Trevor"
$p.FullName()

REST APIs

Invoke-RestMethod -Uri "https://api.github.com/events" -Method Get
Invoke-WebRequest -Uri "https://example.com" -OutFile page.html

System Information

Get-ComputerInfo          # Full system info
Get-Process               # Running processes
Get-Service               # Services list
Get-EventLog -LogName System -Newest 20
Get-WmiObject Win32_OperatingSystem | Select-Object Caption, OSArchitecture

User Management

Get-LocalUser             # List local users
New-LocalUser -Name test -Password (Read-Host -AsSecureString)
Set-LocalUser -Name test -Password (Read-Host -AsSecureString)
Remove-LocalUser -Name test
Add-LocalGroupMember -Group Administrators -Member test
Get-LocalGroupMember Administrators

Aliases

Get-Alias                 # List all aliases
New-Alias ll Get-ChildItem
Set-Alias gs Get-Service
Remove-Item Alias:ll

File Operations

Copy-Item file.txt backup.txt
Move-Item file.txt C:\Archive
Rename-Item old.txt new.txt
Test-Path file.txt

Download Files

Invoke-WebRequest -Uri "https://example.com/file.zip" -OutFile file.zip
Invoke-RestMethod -Uri "https://api.github.com/repos" > repos.json
Start-BitsTransfer -Source "https://example.com/file.iso" -Destination C:\Downloads\file.iso

Error Handling

try {
  1/0
} catch {
  Write-Error "Error: $_"
} finally {
  "Cleanup done"
}

Logging

Start-Transcript -Path log.txt
Write-Output "Logging example"
Stop-Transcript

Security

Get-ExecutionPolicy
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

# Run signed scripts only

Modern PowerShell Features

# Parallel ForEach
1..10 | ForEach-Object -Parallel { $_ * 2 }

# JSON Handling
Get-Process | ConvertTo-Json | Out-File procs.json
Get-Content procs.json | ConvertFrom-Json

# Secrets
Install-Module Microsoft.PowerShell.SecretManagement

✅ This cheatsheet is structured for quick reference, daily scripting, system management, and secure practices. Perfect for both beginners and advanced PowerShell users.