Skip to content

PowerShell Introduction

PowerShell is a cross-platform shell and scripting language built on .NET. It works with objects rather than plain text, making it far more powerful than traditional shells for automation and system administration.

Windows — PowerShell 5.1 is built-in. Install the latest PowerShell 7+:

Terminal window
winget install Microsoft.PowerShell

macOS:

Terminal window
brew install --cask powershell

Linux (Ubuntu/Debian):

Terminal window
sudo apt install powershell

Verify installation:

Terminal window
$PSVersionTable.PSVersion
FeatureCMDBashPowerShell
Object pipelineNoNoYes
.NET integrationNoNoYes
Cross-platformNoYesYes (PS 7+)
Remote managementLimitedSSHBuilt-in (PSSessions)
Module ecosystemNoYesYes (PSGallery)
Terminal window
# Variables — prefix with $
$name = "Alice"
$age = 30
$isAdmin = $true
# String interpolation
Write-Host "Hello, $name! You are $age years old."
# Arrays
$fruits = @("apple", "banana", "cherry")
$fruits[0] # apple
$fruits.Count # 3
# Hash tables
$person = @{
Name = "Alice"
Age = 30
City = "London"
}
$person.Name # Alice
$person["Age"] # 30

PowerShell uses Verb-Noun naming. Common verbs:

VerbPurposeExample
GetRetrieve dataGet-Process, Get-Content
SetChange somethingSet-Location, Set-Content
NewCreate somethingNew-Item, New-Object
RemoveDelete somethingRemove-Item, Remove-Service
StartBegin a processStart-Process, Start-Service
StopEnd a processStop-Process, Stop-Service
InvokeExecuteInvoke-WebRequest, Invoke-Command
Terminal window
# List files
Get-ChildItem # like ls
Get-ChildItem -Recurse *.log # find all .log files
# Read a file
Get-Content .\readme.txt
Get-Content .\readme.txt | Select-Object -First 10 # head -10
# Write to a file
"Hello World" | Out-File .\output.txt
"Appended line" | Add-Content .\output.txt
# Copy, move, delete
Copy-Item .\source.txt .\dest.txt
Move-Item .\old.txt .\new.txt
Remove-Item .\temp.txt
# Create directory
New-Item -ItemType Directory -Path .\logs

PowerShell pipes objects, not text. Each cmdlet receives and passes full .NET objects:

Terminal window
# Get all processes using more than 100MB, sorted by memory
Get-Process | Where-Object { $_.WorkingSet -gt 100MB } | Sort-Object WorkingSet -Descending
# Get the 5 largest files in a directory
Get-ChildItem -Recurse | Sort-Object Length -Descending | Select-Object -First 5 Name, Length
# Count lines in a file
Get-Content .\log.txt | Measure-Object -Line
# Find processes by name
Get-Process | Where-Object Name -like "node*"
Terminal window
# Where-Object — filter
Get-Service | Where-Object Status -eq "Running"
# Select-Object — choose properties
Get-Process | Select-Object Name, Id, CPU | Format-Table
# ForEach-Object — transform
1..5 | ForEach-Object { $_ * 2 } # 2, 4, 6, 8, 10
# Group-Object — group
Get-Process | Group-Object Name | Sort-Object Count -Descending
Terminal window
# Find files modified in the last 7 days
Get-ChildItem -Recurse | Where-Object LastWriteTime -gt (Get-Date).AddDays(-7)
# Kill all node processes
Get-Process node | Stop-Process
# Get public IP address
(Invoke-WebRequest -Uri "https://api.ipify.org").Content
# Extract lines matching a pattern (like grep)
Get-Content .\log.txt | Select-String "ERROR"
# Show disk usage
Get-PSDrive | Where-Object Provider -like "*FileSystem*" | Select-Object Name, Used, Free
# List environment variables
Get-ChildItem Env:
# Encode/decode Base64
[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("hello"))
[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("aGVsbG8="))
Terminal window
# Built-in help system
Get-Help Get-Process
Get-Help Get-Process -Examples
Get-Help Get-Process -Full
# Update help files
Update-Help
# Find commands
Get-Command *process*
Get-Command -Verb Get -Noun *file*