Powershell windows

From findscripter/everything-skills · View interactive version

Powershell windows is an AI agent skill from the GitHub repository findscripter/everything-skills. 当在 Windows 上编写或调试 PowerShell 脚本(含 Agent 生成的自动化脚本)时使用;做规避运算符/Unicode/空值/JSON 等高频陷阱,产出健壮可执行的 .ps1 脚本与命令;不适用于 Bash/Linux shell 脚本或 pwsh 跨平台高级特性教学;触发词:PowerShell、Windows 脚本、.ps1、cmdlet、Test-Path、ConvertTo-Json、ErrorActionPreference

How to use this skill

Skill file

何时使用

在 Windows + PowerShell 环境下编写、生成或调试脚本时,用本技能逐项规避 PowerShell 特有的语法与运行期陷阱,让脚本一次跑通而非反复报「Unexpected token / parameter 'or' / Cannot find property」。尤其适合 Agent 自动生成 .ps1 时做产出前自检。

不该用:

步骤 / 指令

按以下清单逐条核对生成的脚本:

  1. 运算符必须加括号:逻辑运算符两侧的 cmdlet 调用各自用 () 包起来。
  2. 脚本内只用 ASCII:日志/输出禁用 emoji 与 Unicode 符号,改用 [OK] [X] [WARN] [INFO] [...] 标记(Unicode 会触发 Unexpected token)。
  3. 先判空再访问:访问 .Count / .Length / 属性前先确认对象非 $null。
  4. 复杂插值先落变量:深层属性 $obj.prop.sub 先赋给变量再插值。
  5. 显式错误策略 + try/catch:开发用 Stop 快速失败,生产脚本用 Continue;catch 收口、finally 清理,不要在 try 内 return。
  6. 路径用 Join-Path:除非字面常量路径,否则拼接路径一律 Join-Path。
  7. JSON 永远带 -Depth:ConvertTo-Json 默认深度仅 2,嵌套对象必加 -Depth 10。
  8. 数组/集合用正确写法:见示例。
  9. 收尾:用文末脚本模板包裹(StrictMode + 错误策略 + try/catch + exit code)。

示例

运算符(每个 cmdlet 调用都要括号):

# 错误:if (Test-Path "a" -or Test-Path "b")
if ((Test-Path "a") -or (Test-Path "b")) { }
if ((Get-Item $x) -and ($y -eq 5)) { }

空值检查与复杂插值:

# 判空再取 .Count / .Length
if ($array -and $array.Count -gt 0) { }
if ($text) { $text.Length }

# 深层属性先落变量再插值
$value = $obj.prop.sub
Write-Output "Value: $value"

数组与 JSON:

$array = @()                       # 空数组
$array += $item                    # 追加
$list.Add($item) | Out-Null        # ArrayList 追加(吞返回值)

# 读:Get-Content "file.json" -Raw | ConvertFrom-Json
$data | ConvertTo-Json -Depth 10 | Out-File "file.json" -Encoding UTF8

脚本模板(直接套用):

Set-StrictMode -Version Latest
$ErrorActionPreference = "Continue"

$ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path

try {
    # 业务逻辑
    Write-Output "[OK] Done"
    exit 0
}
catch {
    Write-Warning "Error: $_"
    exit 1
}

常见报错速查:

| 报错 | 成因 | 修复 |

|---|---|---|

| parameter 'or' | 逻辑运算符旁的 cmdlet 没加括号 | 把 cmdlet 调用包进 () |

| Unexpected token | 脚本含 Unicode/emoji | 全程改 ASCII |

| Cannot find property | 访问了 $null 对象 | 先判空 |

| Cannot convert | 类型不匹配 | 显式 .ToString() |

注意事项

互见


采编自 sickn33/antigravity-awesome-skills(MIT)。

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.