Get-Help Get-Process | Get help for command |
Get-Help Get-Process -Examples | Show examples |
Get-Command *process* | Find commands |
Get-Alias | List all aliases |
Get-Member | Get object properties |
$PSVersionTable | PowerShell version |
Get-Location (pwd) | Current directory |
Set-Location path (cd) | Change directory |
Get-ChildItem (ls, dir) | List items |
Get-ChildItem -Recurse | List recursively |
Get-ChildItem -Filter *.txt | Filter by extension |
Push-Location / Pop-Location | Stack navigation |
New-Item -ItemType File -Name "file.txt" | Create file |
New-Item -ItemType Directory -Name "folder" | Create folder |
Copy-Item source dest (cp) | Copy item |
Move-Item source dest (mv) | Move item |
Remove-Item path (rm) | Delete item |
Remove-Item -Recurse -Force | Delete folder recursively |
Rename-Item old new | Rename item |
Test-Path path | Check if exists |
Get-Content file.txt (cat) | Read file |
Get-Content file.txt -Tail 10 | Last 10 lines |
Get-Content file.txt -Wait | Follow file (like tail -f) |
Set-Content file.txt "text" | Write to file |
Add-Content file.txt "text" | Append to file |
Clear-Content file.txt | Clear file content |
# Declare variable
$name = "John"
$age = 25
$pi = 3.14
$isValid = $true
# Type casting
[int]$number = "42"
[string]$text = 123
[datetime]$date = "2024-01-01"
# Get variable type
$name.GetType() # Create array
$arr = @(1, 2, 3, 4, 5)
$arr = 1..10 # Range
# Access elements
$arr[0] # First
$arr[-1] # Last
$arr[0..2] # First 3
# Operations
$arr += 6 # Add element
$arr.Count # Length
$arr | ForEach-Object { $_ * 2 } # Create hashtable
$hash = @{
Name = "John"
Age = 25
City = "NYC"
}
# Access values
$hash["Name"]
$hash.Name
# Add/Update
$hash["Country"] = "USA"
$hash.Remove("City")
# Iterate
$hash.GetEnumerator() | ForEach-Object {
"$($_.Key): $($_.Value)"
} # String interpolation
$name = "World"
"Hello, $name!" # Interpolated
'Hello, $name!' # Literal
# Here-strings
$text = @"
Multi-line
string here
"@
# String methods
$str.ToUpper()
$str.ToLower()
$str.Replace("old", "new")
$str.Split(",")
$str.Trim()
$str.Contains("sub") if ($x -gt 10) {
"Greater than 10"
} elseif ($x -eq 10) {
"Equals 10"
} else {
"Less than 10"
}
# Comparison operators
# -eq Equal
# -ne Not equal
# -gt Greater than
# -lt Less than
# -ge Greater or equal
# -le Less or equal
# -like Wildcard match
# -match Regex match switch ($value) {
1 { "One" }
2 { "Two" }
3 { "Three" }
default { "Unknown" }
}
# With wildcard
switch -Wildcard ($file) {
"*.txt" { "Text file" }
"*.ps1" { "Script file" }
}
# With regex
switch -Regex ($text) {
"^\d+$" { "Numbers only" }
"^\w+$" { "Word chars" }
} # For loop
for ($i = 0; $i -lt 10; $i++) {
$i
}
# Foreach loop
foreach ($item in $collection) {
$item
}
# ForEach-Object (pipeline)
1..10 | ForEach-Object {
$_ * 2
}
# Parallel (PowerShell 7+)
1..10 | ForEach-Object -Parallel {
$_ * 2
} -ThrottleLimit 5 # While loop
while ($x -lt 10) {
$x++
}
# Do-While
do {
$x++
} while ($x -lt 10)
# Do-Until
do {
$x++
} until ($x -ge 10)
# Break and Continue
foreach ($i in 1..10) {
if ($i -eq 5) { continue } # Skip
if ($i -eq 8) { break } # Exit loop
$i
} function Say-Hello {
param(
[string]$Name = "World"
)
"Hello, $Name!"
}
Say-Hello -Name "John" function Get-Square {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, ValueFromPipeline=$true)]
[ValidateRange(1, 100)]
[int]$Number
)
begin {
# Run once before pipeline
}
process {
# Run for each pipeline item
$Number * $Number
}
end {
# Run once after pipeline
}
}
1..5 | Get-Square function Get-Data {
$result = @()
$result += "Item 1"
$result += "Item 2"
return $result
}
# Multiple outputs
function Get-Info {
[PSCustomObject]@{
Name = "John"
Age = 25
}
} Get-Process | Where-Object { $_.CPU -gt 100 } | Filter objects |
Get-Process | Sort-Object CPU -Descending | Sort objects |
Get-Process | Select-Object Name, CPU | Select properties |
Get-Process | Select-Object -First 5 | First 5 items |
Get-Process | Group-Object Name | Group objects |
Get-Process | Measure-Object -Property CPU -Sum | Calculate stats |
Get-Process | Format-Table Name, CPU | Format as table |
Get-Process | Format-List * | Format as list |
Get-Process | Export-Csv process.csv | Export to CSV |
Get-Process | ConvertTo-Json | Convert to JSON |
Get-Process | List processes |
Get-Process -Name chrome | Get specific process |
Start-Process notepad | Start process |
Start-Process notepad -Wait | Start and wait |
Stop-Process -Name notepad | Stop process |
Stop-Process -Id 1234 -Force | Force stop by ID |
Get-Service | List services |
Get-Service -Name wuauserv | Get specific service |
Start-Service -Name servicename | Start service |
Stop-Service -Name servicename | Stop service |
Restart-Service -Name servicename | Restart service |
Set-Service -Name service -StartupType Automatic | Set startup type |
Test-Connection google.com | Ping host |
Test-NetConnection -Port 443 google.com | Test port |
Invoke-WebRequest https://api.example.com | HTTP request |
Invoke-RestMethod https://api.example.com/json | REST API call |
Get-NetIPAddress | Get IP addresses |
Get-NetAdapter | List network adapters |
# Enable remoting (admin)
Enable-PSRemoting -Force
# Connect to remote
Enter-PSSession -ComputerName Server01
# Run command remotely
Invoke-Command -ComputerName Server01 -ScriptBlock {
Get-Process
}
# Multiple computers
Invoke-Command -ComputerName Server01, Server02 -ScriptBlock {
$env:COMPUTERNAME
} try {
# Code that might fail
Get-Content "nonexistent.txt" -ErrorAction Stop
}
catch {
# Handle error
Write-Error "Error: $_"
$_.Exception.Message
}
finally {
# Always runs
"Cleanup"
}
# Error action preference
$ErrorActionPreference = "Stop" # Stop, Continue, SilentlyContinue, Inquire