PowerShell is an object-oriented shell and scripting language for system administration and automation.
📁 Navigation and Files
| 📌 Action | 🧠 Command |
|---|
| 📍 Current directory | Get-Location or pwd |
| 📂 Change directory | Set-Location C:\Path or cd C:\Path |
| 📋 List files | Get-ChildItem or ls |
| 🔍 Recursive search | Get-ChildItem -Recurse |
| 📄 Create a file | New-Item -ItemType File file.txt |
| 📁 Create a folder | New-Item -ItemType Directory folder |
| 📋 Copy a file | Copy-Item source.txt destination.txt |
| ➡️ Move a file | Move-Item file.txt C:\Temp\ |
| 🗑️ Delete a file | Remove-Item file.txt |
🔧 Services and Processes
| 📌 Action | 🧠 Command |
|---|
| 📋 List services | Get-Service |
| ▶️ Start a service | Start-Service -Name "ServiceName" |
| ⏹️ Stop a service | Stop-Service -Name "ServiceName" |
| 🔄 Restart a service | Restart-Service -Name "ServiceName" |
| 📊 Service status | Get-Service -Name "ServiceName" |
| 🔍 List processes | Get-Process |
| ❌ Kill a process | Stop-Process -Name "notepad" |
| 💻 Processes by CPU | Get-Process | Sort-Object CPU -Descending |
💾 Variables and Types
| 📌 Action | 🧠 Syntax |
|---|
| 📝 Simple variable | $name = "John" |
| 🔢 Typed variable | [int]$age = 30 |
| 📚 Array | $list = @("item1", "item2", "item3") |
| 🗂️ Hash table | $hash = @{Name="John"; Age=30} |
| 🔤 String | [string]$text = "Hello" |
| ✅ Boolean | [bool]$true_val = $true |
| 📅 Date | $date = Get-Date |
| 🌐 Environment variable | $env:COMPUTERNAME |
🔍 Filtering and Pipeline
| 📌 Action | 🧠 Command |
|---|
| 🔎 Filter with Where | Get-Service | Where-Object {$_.Status -eq "Running"} |
| 📊 Select columns | Get-Process | Select-Object Name, CPU |
| 📈 Sort results | Get-Process | Sort-Object CPU -Descending |
| 📋 First element | Get-Process | Select-Object -First 5 |
| 🎯 Search by name | Get-Service | Where-Object {$_.Name -like "*win*"} |
| 📏 Measure objects | Get-Process | Measure-Object |
| 🔄 ForEach in pipeline | Get-Process | ForEach-Object {$_.Name} |
💬 Display and Output
| 📌 Action | 🧠 Command |
|---|
| 💬 Display text | Write-Host "Hello" |
| 📄 Standard output | Write-Output "Result" |
| ⚠️ Warning | Write-Warning "Attention" |
| ❌ Error | Write-Error "Error" |
| 📝 To a file | Get-Process | Out-File processes.txt |
| 📋 To clipboard | Get-Process | Set-Clipboard |
| 🎨 Colors | Write-Host "Text" -ForegroundColor Red |
🛠️ System Management
| 📌 Action | 🧠 Command |
|---|
| 💻 System info | Get-ComputerInfo |
| 🧠 Available memory | Get-CimInstance Win32_OperatingSystem |
| 💿 Disk space | Get-CimInstance Win32_LogicalDisk |
| 👥 Local users | Get-LocalUser |
| 🔐 Local groups | Get-LocalGroup |
| 🌐 Network configuration | Get-NetAdapter |
| 📡 IP addresses | Get-NetIPAddress |
| 🔄 Restart computer | Restart-Computer -Force |
🔄 Control Structures
Conditions
# Basic If/Else
if ($age -ge 18) {
Write-Host "Adult"
} else {
Write-Host "Minor"
}
# Simple Switch
switch ($day) {
"Monday" { "Start of week" }
"Friday" { "Weekend is near" }
default { "Regular day" }
}
Loops
# Simple ForEach
$services = Get-Service
foreach ($service in $services) {
Write-Host $service.Name
}
# Classic For
for ($i = 1; $i -le 5; $i++) {
Write-Host "Counter: $i"
}
📜 Functions and Scripts
Basic Function
function Get-SystemInfo {
param(
[string]$ComputerName = $env:COMPUTERNAME
)
Write-Host "Information for: $ComputerName"
Get-ComputerInfo | Select-Object TotalPhysicalMemory, CsProcessors
}
# Usage
Get-SystemInfo -ComputerName "MyPC"
Script with Parameters
param(
[Parameter(Mandatory=$true)]
[string]$ServiceName,
[ValidateSet("Start", "Stop", "Restart")]
[string]$Action = "Start"
)
switch ($Action) {
"Start" { Start-Service -Name $ServiceName }
"Stop" { Stop-Service -Name $ServiceName }
"Restart" { Restart-Service -Name $ServiceName }
}
Write-Host "Action $Action performed on $ServiceName"
🛠️ Troubleshooting and Help
| 🆘 Problem | 🧠 Solution |
|---|
| ❓ Help on a command | Get-Help Get-Process -Examples |
| 🔍 Search for a command | Get-Command *service* |
| 📋 View properties | Get-Service | Get-Member |
| 🚫 Execution error | Set-ExecutionPolicy RemoteSigned |
| 📜 Command history | Get-History |
| 🔧 PowerShell version | $PSVersionTable |