What is PowerShell?
PowerShell is Microsoft's command-line shell and scripting language designed for system administration, automation, configuration management, and task orchestration.
Unlike traditional shells that primarily pass text between commands, PowerShell passes objects. This allows commands to work with structured data rather than parsing text output.
PowerShell Editions
- Windows PowerShell 5.1 – Built into Windows.
- PowerShell 7+ – Cross-platform and actively developed.
Launching PowerShell
Windows
- Start Menu → PowerShell
- Windows Terminal
- Win + X menu
Check Version
$PSVersionTable
PowerShell Command Structure
PowerShell commands are called cmdlets.
Verb-Noun
Examples:
Get-Process
Get-Service
Set-Location
Start-Service
Stop-Process
| Verb | Purpose |
|---|---|
| Get | Retrieve data |
| Set | Modify data |
| New | Create resources |
| Remove | Delete resources |
| Start | Launch or begin |
| Stop | Terminate or halt |
Getting Help
Get-Help Get-Process
Get-Help Get-Service -Examples
Get-Help Get-ChildItem -Full
Discover Commands
Get-Command
Get-Command *process*
Get-Command -Verb Get
Variables
$name = "Alice"
$age = 30
Write-Output $name
Write-Output $age
Useful Variables
$HOME
$PWD
$PSVersionTable
Working with Objects
PowerShell returns objects with properties and methods.
Get-Process
Inspect available properties:
Get-Process | Get-Member
Select specific properties:
Get-Process |
Select-Object Name, CPU, Id
The Pipeline
The pipeline (|) passes objects from one command to another.
Get-Service |
Where-Object Status -eq "Running"
Get-Process |
Sort-Object CPU -Descending |
Select-Object -First 10
Files and Directories
Get-ChildItem
Get-ChildItem C:\Logs
New-Item file.txt
New-Item FolderName -ItemType Directory
Copy-Item file.txt backup.txt
Move-Item old.txt archive.txt
Remove-Item temp.txt
Aliases
ls
dir
cd
pwd
Filtering Data
Get-Process |
Where-Object CPU -gt 100
Get-Service |
Where-Object Name -like "*sql*"
Loops
ForEach
$numbers = 1..5
foreach ($n in $numbers) {
Write-Output $n
}
For Loop
for ($i = 0; $i -lt 5; $i++) {
Write-Output $i
}
Conditional Logic
$value = 10
if ($value -gt 5) {
"Greater than 5"
}
else {
"5 or less"
}
Comparison Operators
| Operator | Meaning |
|---|---|
| -eq | Equal |
| -ne | Not equal |
| -gt | Greater than |
| -lt | Less than |
| -like | Pattern matching |
Functions
function Get-Greeting {
param(
[string]$Name
)
"Hello, $Name"
}
Get-Greeting -Name "Alice"
Working with CSV Files
Import-Csv users.csv
Get-Process |
Export-Csv processes.csv -NoTypeInformation
Error Handling
try {
Get-Content missing.txt
}
catch {
Write-Host "File not found"
}
finally {
Write-Host "Finished"
}
Remote Administration
Enter-PSSession Server01
Invoke-Command -ComputerName Server01 `
-ScriptBlock {
Get-Service
}
PowerShell Remoting enables remote management across systems.
Useful Everyday Commands
Get-Process
Get-Service
Get-EventLog
Get-Content
Test-Path
Restart-Service
Stop-Process
Get-NetIPAddress
Get-ComputerInfo
Script Example
# Check free disk space
$drives = Get-PSDrive -PSProvider FileSystem
foreach ($drive in $drives) {
$freeGB = [math]::Round(
$drive.Free / 1GB,
2
)
Write-Output "$($drive.Name): $freeGB GB Free"
}
Best Practices
- Use descriptive variable names.
- Prefer cmdlets over aliases in scripts.
- Use functions to organize code.
- Add comments where necessary.
- Implement error handling.
- Test scripts in non-production environments first.
- Use PowerShell 7 when possible.
Learning Roadmap
- Learn cmdlets and help commands.
- Understand objects and the pipeline.
- Work with files and directories.
- Master filtering and sorting.
- Learn variables, loops, and conditions.
- Write reusable functions.
- Create scripts and automation workflows.
- Explore remoting and administration modules.