P
powershell-windows
by @sickn33v1.0.0
3.7(0)
PowerShell Windows模式。涵盖关键陷阱、运算符语法和错误处理。
安装方式
npx skills add sickn33/antigravity-awesome-skills --skill powershell-windowscompare_arrows
Before / After 效果对比
1 组使用前
编写PowerShell脚本时,若不熟悉其高级特性和常见陷阱,常导致脚本执行效率低下、错误频发,难以实现复杂的系统自动化任务,增加运维负担。
使用后
掌握PowerShell Windows专家模式,运用高级运算符和错误处理策略,编写出更健壮、高效的自动化脚本,显著提升了系统管理效率和任务执行的可靠性。
description SKILL.md
name: powershell-windows description: "PowerShell Windows patterns. Critical pitfalls, operator syntax, error handling." risk: unknown source: community date_added: "2026-02-27"
PowerShell Windows Patterns
Critical patterns and pitfalls for Windows PowerShell.
1. Operator Syntax Rules
CRITICAL: Parentheses Required
| ❌ Wrong | ✅ Correct |
|---|---|
if (Test-Path "a" -or Test-Path "b") | if ((Test-Path "a") -or (Test-Path "b")) |
if (Get-Item $x -and $y -eq 5) | if ((Get-Item $x) -and ($y -eq 5)) |
Rule: Each cmdlet call MUST be in parentheses when using logical operators.
2. Unicode/Emoji Restriction
CRITICAL: No Unicode in Scripts
| Purpose | ❌ Don't Use | ✅ Use |
|---|---|---|
| Success | ✅ ✓ | [OK] [+] |
| Error | ❌ ✗ 🔴 | [!] [X] |
| Warning | ⚠️ 🟡 | [*] [WARN] |
| Info | ℹ️ 🔵 | [i] [INFO] |
| Progress | ⏳ | [...] |
Rule: Use ASCII characters only in PowerShell scripts.
3. Null Check Patterns
Always Check Before Access
| ❌ Wrong | ✅ Correct |
|---|---|
$array.Count -gt 0 | $array -and $array.Count -gt 0 |
$text.Length | if ($text) { $text.Length } |
4. String Interpolation
Complex Expressions
| ❌ Wrong | ✅ Correct |
|---|---|
"Value: $($obj.prop.sub)" | Store in variable first |
Pattern:
$value = $obj.prop.sub
Write-Output "Value: $value"
5. Error Handling
ErrorActionPreference
| Value | Use |
|---|---|
| Stop | Development (fail fast) |
| Continue | Production scripts |
| SilentlyContinue | When errors expected |
Try/Catch Pattern
- Don't return inside try block
- Use finally for cleanup
- Return after try/catch
6. File Paths
Windows Path Rules
| Pattern | Use |
|---|---|
| Literal path | C:\Users\User\file.txt |
| Variable path | Join-Path $env:USERPROFILE "file.txt" |
| Relative | Join-Path $ScriptDir "data" |
Rule: Use Join-Path for cross-platform safety.
7. Array Operations
Correct Patterns
| Operation | Syntax |
|---|---|
| Empty array | $array = @() |
| Add item | $array += $item |
| ArrayList add | `$list.Add($item) |
8. JSON Operations
CRITICAL: Depth Parameter
| ❌ Wrong | ✅ Correct |
|---|---|
ConvertTo-Json | ConvertTo-Json -Depth 10 |
Rule: Always specify -Depth for nested objects.
File Operations
| Operation | Pattern |
|---|---|
| Read | `Get-Content "file.json" -Raw |
| Write | `$data |
9. Common Errors
| Error Message | Cause | Fix |
|---|---|---|
| "parameter 'or'" | Missing parentheses | Wrap cmdlets in () |
| "Unexpected token" | Unicode character | Use ASCII only |
| "Cannot find property" | Null object | Check null first |
| "Cannot convert" | Type mismatch | Use .ToString() |
10. Script Template
# Strict mode
Set-StrictMode -Version Latest
$ErrorActionPreference = "Continue"
# Paths
$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
# Main
try {
# Logic here
Write-Output "[OK] Done"
exit 0
}
catch {
Write-Warning "Error: $_"
exit 1
}
Remember: PowerShell has unique syntax rules. Parentheses, ASCII-only, and null checks are non-negotiable.
When to Use
This skill is applicable to execute the workflow or actions described in the overview.
forum用户评价 (0)
发表评价
效果
易用性
文档
兼容性
暂无评价,来写第一条吧
统计数据
安装量732
评分3.7 / 5.0
版本1.0.0
更新日期2026年3月16日
对比案例1 组
用户评分
3.7(0)
5
0%
4
0%
3
0%
2
0%
1
0%
为此 Skill 评分
0.0
兼容平台
🔧Claude Code
🔧OpenClaw
🔧OpenCode
🔧Codex
🔧Gemini CLI
🔧GitHub Copilot
🔧Amp
🔧Kimi CLI
时间线
创建2026年3月16日
最后更新2026年3月16日