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.
Installation
Section titled “Installation”Windows — PowerShell 5.1 is built-in. Install the latest PowerShell 7+:
winget install Microsoft.PowerShellmacOS:
brew install --cask powershellLinux (Ubuntu/Debian):
sudo apt install powershellVerify installation:
$PSVersionTable.PSVersionPowerShell vs CMD vs Bash
Section titled “PowerShell vs CMD vs Bash”| Feature | CMD | Bash | PowerShell |
|---|---|---|---|
| Object pipeline | No | No | Yes |
| .NET integration | No | No | Yes |
| Cross-platform | No | Yes | Yes (PS 7+) |
| Remote management | Limited | SSH | Built-in (PSSessions) |
| Module ecosystem | No | Yes | Yes (PSGallery) |
Basic Syntax
Section titled “Basic Syntax”# Variables — prefix with $$name = "Alice"$age = 30$isAdmin = $true
# String interpolationWrite-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"] # 30Cmdlet Naming Convention
Section titled “Cmdlet Naming Convention”PowerShell uses Verb-Noun naming. Common verbs:
| Verb | Purpose | Example |
|---|---|---|
| Get | Retrieve data | Get-Process, Get-Content |
| Set | Change something | Set-Location, Set-Content |
| New | Create something | New-Item, New-Object |
| Remove | Delete something | Remove-Item, Remove-Service |
| Start | Begin a process | Start-Process, Start-Service |
| Stop | End a process | Stop-Process, Stop-Service |
| Invoke | Execute | Invoke-WebRequest, Invoke-Command |
Working with Files
Section titled “Working with Files”# List filesGet-ChildItem # like lsGet-ChildItem -Recurse *.log # find all .log files
# Read a fileGet-Content .\readme.txtGet-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, deleteCopy-Item .\source.txt .\dest.txtMove-Item .\old.txt .\new.txtRemove-Item .\temp.txt
# Create directoryNew-Item -ItemType Directory -Path .\logsThe Pipeline
Section titled “The Pipeline”PowerShell pipes objects, not text. Each cmdlet receives and passes full .NET objects:
# Get all processes using more than 100MB, sorted by memoryGet-Process | Where-Object { $_.WorkingSet -gt 100MB } | Sort-Object WorkingSet -Descending
# Get the 5 largest files in a directoryGet-ChildItem -Recurse | Sort-Object Length -Descending | Select-Object -First 5 Name, Length
# Count lines in a fileGet-Content .\log.txt | Measure-Object -Line
# Find processes by nameGet-Process | Where-Object Name -like "node*"Filtering and Selecting
Section titled “Filtering and Selecting”# Where-Object — filterGet-Service | Where-Object Status -eq "Running"
# Select-Object — choose propertiesGet-Process | Select-Object Name, Id, CPU | Format-Table
# ForEach-Object — transform1..5 | ForEach-Object { $_ * 2 } # 2, 4, 6, 8, 10
# Group-Object — groupGet-Process | Group-Object Name | Sort-Object Count -DescendingUseful One-Liners
Section titled “Useful One-Liners”# Find files modified in the last 7 daysGet-ChildItem -Recurse | Where-Object LastWriteTime -gt (Get-Date).AddDays(-7)
# Kill all node processesGet-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 usageGet-PSDrive | Where-Object Provider -like "*FileSystem*" | Select-Object Name, Used, Free
# List environment variablesGet-ChildItem Env:
# Encode/decode Base64[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes("hello"))[Text.Encoding]::UTF8.GetString([Convert]::FromBase64String("aGVsbG8="))Getting Help
Section titled “Getting Help”# Built-in help systemGet-Help Get-ProcessGet-Help Get-Process -ExamplesGet-Help Get-Process -Full
# Update help filesUpdate-Help
# Find commandsGet-Command *process*Get-Command -Verb Get -Noun *file*