- Basic Commands
- File System Operations
- Process Management
- Network Operations
- Variables and Data Types
- Flow Control
- Functions and Modules
- Error Handling
- Best Practices
# Get System Information
Get-ComputerInfo
# Get PowerShell Version
$PSVersionTable
# Get Current Location
Get-Location # or pwd# Get help for a command
Get-Help Get-Process
Get-Help Get-Process -Examples
Get-Help Get-Process -Detailed
Get-Help Get-Process -Full
# Update help documentation
Update-Help# Navigate directories
Set-Location path\to\directory # or cd
Get-Location # or pwd
# Create/Remove directories
New-Item -ItemType Directory -Path "NewFolder"
Remove-Item -Path "FolderToDelete" -Recurse
# List items
Get-ChildItem # or dir or ls
Get-ChildItem -Path C:\ -Recurse -Filter *.txt# Create/Delete files
New-Item -ItemType File -Path "test.txt"
Remove-Item "file.txt"
# Copy/Move files
Copy-Item "source.txt" -Destination "dest.txt"
Move-Item "old.txt" "new.txt"
# Read/Write files
Get-Content "file.txt"
Set-Content "file.txt" "New content"
Add-Content "file.txt" "Appended content"# List all processes
Get-Process
# Start a new process
Start-Process notepad.exe
# Stop a process
Stop-Process -Name "notepad"
Stop-Process -Id 1234
# Get service status
Get-Service# Test network connection
Test-NetConnection google.com
Test-NetConnection -ComputerName server01 -Port 80
# Get IP configuration
Get-NetIPConfiguration
Get-NetAdapter
# Download file from internet
Invoke-WebRequest -Uri "https://example.com/file.zip" -OutFile "file.zip"# Variable declaration
$name = "John"
$age = 30
$isActive = $true
$numbers = @(1, 2, 3, 4, 5)
$hash = @{
Key1 = "Value1"
Key2 = "Value2"
}
# Type casting
[int]"123"
[string]256
[datetime]"2024-01-01"# If statement
if ($age -gt 18) {
"Adult"
} elseif ($age -eq 18) {
"Just turned adult"
} else {
"Minor"
}
# Switch statement
switch ($value) {
1 { "One" }
2 { "Two" }
default { "Other" }
}# ForEach loop
foreach ($item in $collection) {
$item
}
# For loop
for ($i = 0; $i -lt 5; $i++) {
"Iteration $i"
}
# While loop
while ($condition) {
# code
}# Function definition
function Get-Square {
param(
[Parameter(Mandatory=$true)]
[int]$Number
)
return $Number * $Number
}
# Module operations
Get-Module -ListAvailable
Import-Module ModuleName
Get-Command -Module ModuleName# Try-Catch block
try {
# Risky operation
$result = 1/0
} catch {
Write-Error "An error occurred: $_"
} finally {
# Cleanup code
}
# Error preference
$ErrorActionPreference = "Stop" # or "Continue", "SilentlyContinue"- Use approved verbs for function names (Get-, Set-, New-, Remove-, etc.)
- Follow consistent naming conventions (PascalCase for functions, camelCase for variables)
- Add comments for complex operations
- Use proper indentation and spacing
- Use pipeline operations when possible
- Avoid unnecessary type conversions
- Use proper filtering at the source
- Consider using background jobs for long-running operations
- Always validate input
- Use secure string for sensitive data
- Follow principle of least privilege
- Implement proper error handling
This quick reference guide is released under the MIT License. Feel free to use, modify, and distribute as needed.
Made with β€οΈ by Selam