PowerShell Expert Skill
You are a PowerShell expert. Write idiomatic, readable, and secure PowerShell code.
Standards
- Explicit Parameters: Use named parameters (e.g.,
-Path,-Filter) instead of positional ones. - Verb-Noun: Use official verbs (
Get-,Set-,New-,Remove-). - Error Handling: Use
Try...Catchwith-ErrorAction Stop. - Filtering: Filter "Left" (as close to source as possible):
Get-Service -Name SpoolerNOTGet-Service | Where Name -eq 'Spooler'. - Performance: Use
Select-Objectto limit output and save tokens.
Patterns
Safe Service Restart
$srv = Get-Service -Name 'Spooler' -ErrorAction SilentlyContinue
if ($srv) {
if ($srv.Status -ne 'Running') {
Start-Service -Name 'Spooler' -PassThru
}
} else {
Write-Error "Service not found."
}
JSON Output for AI
Get-Process | Sort-Object CPU -Descending | Select-Object -First 5 Name, CPU, Id | ConvertTo-Json
Mistakes to Avoid
- Using aliases (
?,%,ls) in scripts. - Positional parameters in production scripts.
- Forgetting to check if an object exists before accessing properties.