Get-Helpalias: help Getting Help
Display help for any cmdlet or topic
Get-Help Get-Process -Full
Get-Help about_Variables copy Get-Commandalias: gcm Getting Help
Find any command by name or pattern
Get-Command *Service*
Get-Command -Verb Get -Noun *Process* copy Get-Memberalias: gm Getting Help
Show properties and methods of an object
Get-Process | Get-Member
'hello' | Get-Member copy Download latest help files from internet
Set-Locationalias: cd / sl Files & Dirs
Change current directory
Set-Location C:\Users
cd ~
cd - # go back copy Get-ChildItemalias: ls / dir / gci Files & Dirs
List files and directories
Get-ChildItem -Recurse -Filter *.txt
ls -Hidden copy Get-Itemalias: gi Files & Dirs
Get a file or directory object
Get-Item C:\Windows
(Get-Item file.txt).Length copy Copy-Itemalias: cp / copy Files & Dirs
Copy files or directories
Copy-Item file.txt backup.txt
Copy-Item -Recurse src\ dst\ copy Move-Itemalias: mv / move Files & Dirs
Move or rename files
Move-Item old.txt new.txt
Move-Item *.log archive\ copy Remove-Itemalias: rm / del Files & Dirs
Delete files or directories
Remove-Item file.txt
Remove-Item -Recurse -Force folder\ copy New-Itemalias: ni Files & Dirs
Create new file or directory
New-Item file.txt -ItemType File
New-Item myfolder -ItemType Directory copy Get-Contentalias: cat / gc Files & Dirs
Read file contents
Get-Content log.txt
Get-Content log.txt -Tail 20 -Wait copy Set-Contentalias: sc Files & Dirs
Write content to a file (overwrites)
Set-Content out.txt "Hello World"
1..10 | Set-Content numbers.txt copy Add-Contentalias: ac Files & Dirs
Append content to a file
Add-Content log.txt "New entry"
"line" | Add-Content file.txt copy Check if a path exists (returns $true/$false)
if (Test-Path "C:\file.txt") { "exists" } copy Resolve-Pathalias: rvpa Files & Dirs
Resolve full absolute path
Resolve-Path .\script.ps1 copy Declare and use a variable
$name = "Alice"
$num = 42
$arr = @(1,2,3)
$map = @{ key = "val" } copy Type casting and typed variables
[int]$x = "42"
[string]$s = 123
[datetime]"2024-01-01" copy Get-Variablealias: gv Variables
List or get variable values
Get-Variable
Get-Variable PSVersionTable copy Remove-Variablealias: rv Variables
Delete a variable
Remove-Variable name
$x = $null # set to null copy Built-in automatic variables
$_ # current pipeline object
$? # last command success
$! # last error
$PSVersionTable
$HOME $PWD $env:PATH copy Where-Objectalias: where / ? Pipeline
Filter objects by condition
Get-Process | Where-Object CPU -gt 10
Get-Process | ? { $_.Name -like 'chrome*' } copy Select-Objectalias: select Pipeline
Select specific properties or first/last N
Get-Process | Select-Object Name, CPU, Id
Get-Process | Select-Object -First 5 copy Sort-Objectalias: sort Pipeline
Sort pipeline objects
Get-Process | Sort-Object CPU -Descending
Get-ChildItem | Sort-Object LastWriteTime copy ForEach-Objectalias: foreach / % Pipeline
Run script block for each item
1..5 | ForEach-Object { $_ * 2 }
Get-Process | % { Write-Host $_.Name } copy Group-Objectalias: group Pipeline
Group objects by property
Get-Process | Group-Object -Property Company
Get-ChildItem | Group-Object Extension copy Measure-Objectalias: measure Pipeline
Calculate sum, average, min, max, count
1..100 | Measure-Object -Sum -Average
Get-ChildItem | Measure-Object Length -Sum copy Tee-Objectalias: tee Pipeline
Send output to file AND pipeline
Get-Process | Tee-Object -FilePath procs.txt | Select Name copy Common string methods
$s = "Hello World"
$s.ToUpper() # HELLO WORLD
$s.Split(" ") # @("Hello","World")
$s.Replace("o","0") # Hell0 W0rld
$s.Trim() $s.Length $s.Contains("lo") copy Wildcard and regex matching
"PowerShell" -like "Power*" # $true
"abc123" -match "\d+" # $true
$Matches[0] # "123" copy Regex replace on strings
"hello world" -replace "o","0" # hell0 w0rld
"abc123" -replace "\d+","NUM" # abcNUM copy Multi-line string literal
@"
Line one
Line two
"@ copy Format strings with -f operator
"{0} has {1} items" -f "Cart", 5
"Pi = {0:F4}" -f [Math]::PI copy if / elseif / elseLoops & Flow
Conditional branching
if ($x -gt 10) {
"big"
} elseif ($x -gt 5) {
"medium"
} else {
"small"
} copy Multi-branch switch statement
switch ($day) {
"Mon" { "Monday" }
"Fri" { "Friday" }
default { "Other" }
} copy Iterate over a collection
foreach ($file in Get-ChildItem *.txt) {
Write-Host $file.Name
} copy Classic C-style for loop
for ($i = 0; $i -lt 10; $i++) {
Write-Host $i
} copy while / do-whileLoops & Flow
Loop while condition is true
$i = 0
while ($i -lt 5) { $i++ }
do { $i-- } while ($i -gt 0) copy break / continueLoops & Flow
Exit loop or skip to next iteration
foreach ($n in 1..10) {
if ($n -eq 5) { break } # exit loop
if ($n % 2 -eq 0) { continue } # skip even
Write-Host $n
} copy Define a reusable function
function Greet {
param([string]$Name = "World")
"Hello, $Name!"
}
Greet -Name "Alice" copy Advanced functionFunctions
Function with CmdletBinding and validation
function Get-Square {
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[ValidateRange(1,100)]
[int]$Number
)
return $Number * $Number
} copy Function that accepts pipeline input
function Double {
param([Parameter(ValueFromPipeline)][int]$n)
process { $n * 2 }
}
1..5 | Double copy try / catch / finallyError Handling
Structured exception handling
try {
Get-Item "nofile.txt" -ErrorAction Stop
} catch [System.IO.FileNotFoundException] {
Write-Error "Not found: $_"
} finally {
"Always runs"
} copy -ErrorActionError Handling
Control error behavior per cmdlet
Get-Item missing.txt -ErrorAction SilentlyContinue
Get-Item missing.txt -ErrorAction Stop # throws copy $ErrorActionPreferenceError Handling
Set default error action globally
$ErrorActionPreference = "Stop" # throw on all errors
$ErrorActionPreference = "Continue" # default copy Get-Processalias: ps / gps Processes
List running processes
Get-Process
Get-Process -Name chrome
Get-Process | Sort-Object CPU -Desc | Select -First 10 copy Stop-Processalias: kill Processes
Kill a process by name or PID
Stop-Process -Name notepad
Stop-Process -Id 1234 -Force copy Start-Processalias: start Processes
Launch a program
Start-Process notepad.exe
Start-Process cmd -ArgumentList "/c dir" -Wait copy Get-Servicealias: gsv Processes
List Windows services
Get-Service
Get-Service -Name wuauserv
Get-Service | Where Status -eq Running copy Start/Stop-ServiceProcesses
Start, stop, or restart a service
Start-Service wuauserv
Stop-Service -Name Spooler
Restart-Service -Name Spooler copy Test-Connectionalias: ping Network
Ping a host (PowerShell ping)
Test-Connection google.com
Test-Connection 8.8.8.8 -Count 2 -Quiet copy Invoke-WebRequestalias: iwr / curl / wget Network
Make HTTP requests
Invoke-WebRequest https://api.github.com/zen
$r = iwr "https://example.com"
$r.Content copy Invoke-RestMethodalias: irm Network
Make REST API calls, auto-parse JSON
$data = Invoke-RestMethod "https://api.github.com/users/torvalds"
$data.public_repos copy Test-NetConnectionalias: tnc Network
Test TCP port connectivity
Test-NetConnection google.com -Port 443
tnc 192.168.1.1 -Port 22 copy Get system information
Get-ComputerInfo
Get-ComputerInfo -Property OsName, TotalPhysicalMemory copy Get current date/time, format dates
Get-Date
Get-Date -Format "yyyy-MM-dd HH:mm:ss"
(Get-Date).AddDays(-7) copy Read and set environment variables
$env:PATH
$env:USERPROFILE
$env:MyVar = "value"
[System.Environment]::GetEnvironmentVariable("PATH") copy Read Windows Event Log (PS 5.1)
Get-EventLog -LogName System -Newest 20
Get-EventLog -LogName Application -EntryType Error copy Interactive remote PowerShell session
Enter-PSSession -ComputerName server01
Enter-PSSession -ComputerName 192.168.1.5 -Credential (Get-Credential) copy Invoke-Commandalias: icm Remoting
Run script block on remote computer
Invoke-Command -ComputerName server01 -ScriptBlock { Get-Process }
Invoke-Command -ComputerName s1,s2 -FilePath .script.ps1 copy Create persistent remote session
$s = New-PSSession -ComputerName server01
Invoke-Command -Session $s { hostname }
Remove-PSSession $s copy