PowerShell Windows patterns. Critical pitfalls, operator syntax, error handling.
Add this skill
npx mdskills install sickn33/powershell-windowsComprehensive PowerShell reference with critical syntax rules, error patterns, and actionable examples.
1---2name: powershell-windows3description: PowerShell Windows patterns. Critical pitfalls, operator syntax, error handling.4allowed-tools: Read, Write, Edit, Glob, Grep, Bash5---67# PowerShell Windows Patterns89> Critical patterns and pitfalls for Windows PowerShell.1011---1213## 1. Operator Syntax Rules1415### CRITICAL: Parentheses Required1617| ❌ Wrong | ✅ Correct |18|----------|-----------|19| `if (Test-Path "a" -or Test-Path "b")` | `if ((Test-Path "a") -or (Test-Path "b"))` |20| `if (Get-Item $x -and $y -eq 5)` | `if ((Get-Item $x) -and ($y -eq 5))` |2122**Rule:** Each cmdlet call MUST be in parentheses when using logical operators.2324---2526## 2. Unicode/Emoji Restriction2728### CRITICAL: No Unicode in Scripts2930| Purpose | ❌ Don't Use | ✅ Use |31|---------|-------------|--------|32| Success | ✅ ✓ | [OK] [+] |33| Error | ❌ ✗ 🔴 | [!] [X] |34| Warning | ⚠️ 🟡 | [*] [WARN] |35| Info | ℹ️ 🔵 | [i] [INFO] |36| Progress | ⏳ | [...] |3738**Rule:** Use ASCII characters only in PowerShell scripts.3940---4142## 3. Null Check Patterns4344### Always Check Before Access4546| ❌ Wrong | ✅ Correct |47|----------|-----------|48| `$array.Count -gt 0` | `$array -and $array.Count -gt 0` |49| `$text.Length` | `if ($text) { $text.Length }` |5051---5253## 4. String Interpolation5455### Complex Expressions5657| ❌ Wrong | ✅ Correct |58|----------|-----------|59| `"Value: $($obj.prop.sub)"` | Store in variable first |6061**Pattern:**62```63$value = $obj.prop.sub64Write-Output "Value: $value"65```6667---6869## 5. Error Handling7071### ErrorActionPreference7273| Value | Use |74|-------|-----|75| Stop | Development (fail fast) |76| Continue | Production scripts |77| SilentlyContinue | When errors expected |7879### Try/Catch Pattern8081- Don't return inside try block82- Use finally for cleanup83- Return after try/catch8485---8687## 6. File Paths8889### Windows Path Rules9091| Pattern | Use |92|---------|-----|93| Literal path | `C:\Users\User\file.txt` |94| Variable path | `Join-Path $env:USERPROFILE "file.txt"` |95| Relative | `Join-Path $ScriptDir "data"` |9697**Rule:** Use Join-Path for cross-platform safety.9899---100101## 7. Array Operations102103### Correct Patterns104105| Operation | Syntax |106|-----------|--------|107| Empty array | `$array = @()` |108| Add item | `$array += $item` |109| ArrayList add | `$list.Add($item) | Out-Null` |110111---112113## 8. JSON Operations114115### CRITICAL: Depth Parameter116117| ❌ Wrong | ✅ Correct |118|----------|-----------|119| `ConvertTo-Json` | `ConvertTo-Json -Depth 10` |120121**Rule:** Always specify `-Depth` for nested objects.122123### File Operations124125| Operation | Pattern |126|-----------|---------|127| Read | `Get-Content "file.json" -Raw | ConvertFrom-Json` |128| Write | `$data | ConvertTo-Json -Depth 10 | Out-File "file.json" -Encoding UTF8` |129130---131132## 9. Common Errors133134| Error Message | Cause | Fix |135|---------------|-------|-----|136| "parameter 'or'" | Missing parentheses | Wrap cmdlets in () |137| "Unexpected token" | Unicode character | Use ASCII only |138| "Cannot find property" | Null object | Check null first |139| "Cannot convert" | Type mismatch | Use .ToString() |140141---142143## 10. Script Template144145```powershell146# Strict mode147Set-StrictMode -Version Latest148$ErrorActionPreference = "Continue"149150# Paths151$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path152153# Main154try {155 # Logic here156 Write-Output "[OK] Done"157 exit 0158}159catch {160 Write-Warning "Error: $_"161 exit 1162}163```164165---166167> **Remember:** PowerShell has unique syntax rules. Parentheses, ASCII-only, and null checks are non-negotiable.168
Full transparency — inspect the skill content before installing.