$npx -y skills add One-Man-Company/Skills-ContextManager --skill powershell-windowsPowerShell Windows patterns. Critical pitfalls, operator syntax, error handling.
| 1 | # PowerShell Windows Patterns |
| 2 | |
| 3 | > Critical patterns and pitfalls for Windows PowerShell. |
| 4 | |
| 5 | --- |
| 6 | |
| 7 | ## 1. Operator Syntax Rules |
| 8 | |
| 9 | ### CRITICAL: Parentheses Required |
| 10 | |
| 11 | | ❌ Wrong | ✅ Correct | |
| 12 | |----------|-----------| |
| 13 | | `if (Test-Path "a" -or Test-Path "b")` | `if ((Test-Path "a") -or (Test-Path "b"))` | |
| 14 | | `if (Get-Item $x -and $y -eq 5)` | `if ((Get-Item $x) -and ($y -eq 5))` | |
| 15 | |
| 16 | **Rule:** Each cmdlet call MUST be in parentheses when using logical operators. |
| 17 | |
| 18 | --- |
| 19 | |
| 20 | ## 2. Unicode/Emoji Restriction |
| 21 | |
| 22 | ### CRITICAL: No Unicode in Scripts |
| 23 | |
| 24 | | Purpose | ❌ Don't Use | ✅ Use | |
| 25 | |---------|-------------|--------| |
| 26 | | Success | ✅ ✓ | [OK] [+] | |
| 27 | | Error | ❌ ✗ 🔴 | [!] [X] | |
| 28 | | Warning | ⚠️ 🟡 | [*] [WARN] | |
| 29 | | Info | ℹ️ 🔵 | [i] [INFO] | |
| 30 | | Progress | ⏳ | [...] | |
| 31 | |
| 32 | **Rule:** Use ASCII characters only in PowerShell scripts. |
| 33 | |
| 34 | --- |
| 35 | |
| 36 | ## 3. Null Check Patterns |
| 37 | |
| 38 | ### Always Check Before Access |
| 39 | |
| 40 | | ❌ Wrong | ✅ Correct | |
| 41 | |----------|-----------| |
| 42 | | `$array.Count -gt 0` | `$array -and $array.Count -gt 0` | |
| 43 | | `$text.Length` | `if ($text) { $text.Length }` | |
| 44 | |
| 45 | --- |
| 46 | |
| 47 | ## 4. String Interpolation |
| 48 | |
| 49 | ### Complex Expressions |
| 50 | |
| 51 | | ❌ Wrong | ✅ Correct | |
| 52 | |----------|-----------| |
| 53 | | `"Value: $($obj.prop.sub)"` | Store in variable first | |
| 54 | |
| 55 | **Pattern:** |
| 56 | ``` |
| 57 | $value = $obj.prop.sub |
| 58 | Write-Output "Value: $value" |
| 59 | ``` |
| 60 | |
| 61 | --- |
| 62 | |
| 63 | ## 5. Error Handling |
| 64 | |
| 65 | ### ErrorActionPreference |
| 66 | |
| 67 | | Value | Use | |
| 68 | |-------|-----| |
| 69 | | Stop | Development (fail fast) | |
| 70 | | Continue | Production scripts | |
| 71 | | SilentlyContinue | When errors expected | |
| 72 | |
| 73 | ### Try/Catch Pattern |
| 74 | |
| 75 | - Don't return inside try block |
| 76 | - Use finally for cleanup |
| 77 | - Return after try/catch |
| 78 | |
| 79 | --- |
| 80 | |
| 81 | ## 6. File Paths |
| 82 | |
| 83 | ### Windows Path Rules |
| 84 | |
| 85 | | Pattern | Use | |
| 86 | |---------|-----| |
| 87 | | Literal path | `C:\Users\User\file.txt` | |
| 88 | | Variable path | `Join-Path $env:USERPROFILE "file.txt"` | |
| 89 | | Relative | `Join-Path $ScriptDir "data"` | |
| 90 | |
| 91 | **Rule:** Use Join-Path for cross-platform safety. |
| 92 | |
| 93 | --- |
| 94 | |
| 95 | ## 7. Array Operations |
| 96 | |
| 97 | ### Correct Patterns |
| 98 | |
| 99 | | Operation | Syntax | |
| 100 | |-----------|--------| |
| 101 | | Empty array | `$array = @()` | |
| 102 | | Add item | `$array += $item` | |
| 103 | | ArrayList add | `$list.Add($item) | Out-Null` | |
| 104 | |
| 105 | --- |
| 106 | |
| 107 | ## 8. JSON Operations |
| 108 | |
| 109 | ### CRITICAL: Depth Parameter |
| 110 | |
| 111 | | ❌ Wrong | ✅ Correct | |
| 112 | |----------|-----------| |
| 113 | | `ConvertTo-Json` | `ConvertTo-Json -Depth 10` | |
| 114 | |
| 115 | **Rule:** Always specify `-Depth` for nested objects. |
| 116 | |
| 117 | ### File Operations |
| 118 | |
| 119 | | Operation | Pattern | |
| 120 | |-----------|---------| |
| 121 | | Read | `Get-Content "file.json" -Raw | ConvertFrom-Json` | |
| 122 | | Write | `$data | ConvertTo-Json -Depth 10 | Out-File "file.json" -Encoding UTF8` | |
| 123 | |
| 124 | --- |
| 125 | |
| 126 | ## 9. Common Errors |
| 127 | |
| 128 | | Error Message | Cause | Fix | |
| 129 | |---------------|-------|-----| |
| 130 | | "parameter 'or'" | Missing parentheses | Wrap cmdlets in () | |
| 131 | | "Unexpected token" | Unicode character | Use ASCII only | |
| 132 | | "Cannot find property" | Null object | Check null first | |
| 133 | | "Cannot convert" | Type mismatch | Use .ToString() | |
| 134 | |
| 135 | --- |
| 136 | |
| 137 | ## 10. Script Template |
| 138 | |
| 139 | ```powershell |
| 140 | # Strict mode |
| 141 | Set-StrictMode -Version Latest |
| 142 | $ErrorActionPreference = "Continue" |
| 143 | |
| 144 | # Paths |
| 145 | $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path |
| 146 | |
| 147 | # Main |
| 148 | try { |
| 149 | # Logic here |
| 150 | Write-Output "[OK] Done" |
| 151 | exit 0 |
| 152 | } |
| 153 | catch { |
| 154 | Write-Warning "Error: $_" |
| 155 | exit 1 |
| 156 | } |
| 157 | ``` |
| 158 | |
| 159 | --- |
| 160 | |
| 161 | > **Remember:** PowerShell has unique syntax rules. Parentheses, ASCII-only, and null checks are non-negotiable. |