Powershell
From Basics Fundamentals
Go to text ā
PowerShell Basics Guide
Core Philosophy
PowerShell works with objects, not text. Commands pass structured data between each other, making scripting more reliable and powerful.
Command Structure
Verb-Noun Format
All cmdlets follow a consistent Verb-Noun pattern:
Get-Process- retrieve processesSet-Location- change directoryStop-Service- stop a serviceNew-Item- create a file or folder
Common Verbs
- Get - retrieve data
- Set - change configuration
- New - create something
- Remove - delete something
- Start/Stop - control services/processes
- Import/Export - work with data
Getting Help
PowerShell has excellent built-in documentation:
# Get help for any command
Get-Help Get-Process
# Get detailed help with examples
Get-Help Get-Process -Full
Get-Help Get-Process -Examples
# Update help files (run once)
Update-Help
# Find commands
Get-Command *process*
Get-Command -Verb Get
Get-Command -Noun Service
The Pipeline
The pipeline (|) passes entire objects between commands:
# Get processes, filter by CPU usage, select specific properties
Get-Process | Where-Object {$_.CPU -gt 10} | Select-Object Name, CPU, Memory
# Get services that are running
Get-Service | Where-Object {$_.Status -eq "Running"}
# Get files larger than 1MB
Get-ChildItem | Where-Object {$_.Length -gt 1MB}
Variables
Variables start with $:
$name = "John"
$number = 42
$processes = Get-Process
# Access object properties
$processes[0].Name
Working with Objects
Every object has properties and methods:
# Get an object
$proc = Get-Process -Name "chrome" | Select-Object -First 1
# View all properties
$proc | Get-Member
# Access properties
$proc.Name
$proc.CPU
$proc.WorkingSet
# Call methods
$proc.Kill()
Common Commands
File System
# List files (like ls or dir)
Get-ChildItem
Get-ChildItem -Recurse # recursive
Get-ChildItem *.txt # filter
# Change directory
Set-Location C:\Users
cd C:\Users # alias works too
# Create file/folder
New-Item -Path "test.txt" -ItemType File
New-Item -Path "newfolder" -ItemType Directory
# Copy, Move, Remove
Copy-Item source.txt destination.txt
Move-Item old.txt new.txt
Remove-Item file.txt
Processes
# List processes
Get-Process
# Get specific process
Get-Process -Name chrome
# Stop process
Stop-Process -Name notepad
Stop-Process -Id 1234
Services
# List services
Get-Service
# Control services
Start-Service -Name "wuauserv"
Stop-Service -Name "wuauserv"
Restart-Service -Name "wuauserv"
Filtering and Selecting
Where-Object
Filters objects in the pipeline:
# Filter by condition
Get-Process | Where-Object {$_.CPU -gt 100}
Get-Service | Where-Object {$_.Status -eq "Stopped"}
Get-ChildItem | Where-Object {$_.Length -lt 1KB}
# Shorter syntax (PowerShell 3.0+)
Get-Process | Where CPU -gt 100
Select-Object
Chooses which properties to display:
# Select specific properties
Get-Process | Select-Object Name, CPU, Memory
# Select first/last items
Get-Process | Select-Object -First 5
Get-Process | Select-Object -Last 3
Comparison Operators
-eq # equal
-ne # not equal
-gt # greater than
-lt # less than
-ge # greater than or equal
-le # less than or equal
-like # wildcard match
-match # regex match
Aliases
PowerShell includes familiar aliases:
ls # Get-ChildItem
dir # Get-ChildItem
cd # Set-Location
pwd # Get-Location
cat # Get-Content
cp # Copy-Item
mv # Move-Item
rm # Remove-Item
man # Get-Help
# Find aliases
Get-Alias
Get-Alias -Name ls
Output and Export
# Display as table
Get-Process | Format-Table
# Display as list
Get-Process | Format-List
# Export to CSV
Get-Process | Export-Csv processes.csv
# Export to JSON
Get-Process | ConvertTo-Json | Out-File processes.json
# Save to text file
Get-Process | Out-File processes.txt
Basic Scripting
If Statements
$value = 10
if ($value -gt 5) {
Write-Host "Greater than 5"
} elseif ($value -eq 5) {
Write-Host "Equal to 5"
} else {
Write-Host "Less than 5"
}
Loops
# ForEach
$items = 1..5
foreach ($item in $items) {
Write-Host $item
}
# ForEach-Object (in pipeline)
Get-Process | ForEach-Object {
Write-Host $_.Name
}
# While
$i = 0
while ($i -lt 5) {
Write-Host $i
$i++
}
Practical Examples
Find large files
Get-ChildItem -Recurse |
Where-Object {$_.Length -gt 100MB} |
Select-Object Name, Length, FullName |
Sort-Object Length -Descending
Monitor CPU usage
Get-Process |
Sort-Object CPU -Descending |
Select-Object -First 10 Name, CPU, WorkingSet
Find stopped services
Get-Service |
Where-Object {$_.Status -eq "Stopped"} |
Select-Object Name, DisplayName
Check disk space
Get-PSDrive -PSProvider FileSystem |
Select-Object Name, Used, Free,
@{Name="UsedGB";Expression={[math]::Round($_.Used/1GB,2)}},
@{Name="FreeGB";Expression={[math]::Round($_.Free/1GB,2)}}
Tips
- Tab completion - Press Tab to autocomplete commands and parameters
- Use Get-Member - Explore what properties and methods objects have
- ISE or VS Code - Use PowerShell ISE or VS Code for script editing
- Execution Policy - You may need to run
Set-ExecutionPolicy RemoteSignedto run scripts - Case insensitive - PowerShell commands are not case-sensitive
- Comments - Use
#for single-line comments,<# #>for multi-line
Next Steps
- Learn about functions and modules
- Explore PowerShell remoting for managing remote computers
- Study error handling with try/catch
- Look into PowerShell profiles for customization
- Practice with real automation tasks
Children