powershell-windows by davila7/claude-code-templates
npx skills add https://github.com/davila7/claude-code-templates --skill powershell-windowsWindows PowerShell 的关键模式与陷阱。
| ❌ 错误 | ✅ 正确 |
|---|---|
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)) |
规则: 使用逻辑运算符时,每个 cmdlet 调用必须放在括号内。
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
| 用途 | ❌ 不要使用 | ✅ 使用 |
|---|---|---|
| 成功 | ✅ ✓ | [OK] [+] |
| 错误 | ❌ ✗ 🔴 | [!] [X] |
| 警告 | ⚠️ 🟡 | [*] [WARN] |
| 信息 | ℹ️ 🔵 | [i] [INFO] |
| 进度 | ⏳ | [...] |
规则: 在 PowerShell 脚本中仅使用 ASCII 字符。
| ❌ 错误 | ✅ 正确 |
|---|---|
$array.Count -gt 0 | $array -and $array.Count -gt 0 |
$text.Length | if ($text) { $text.Length } |
| ❌ 错误 | ✅ 正确 |
|---|---|
"Value: $($obj.prop.sub)" | 先存储在变量中 |
模式:
$value = $obj.prop.sub
Write-Output "Value: $value"
| 值 | 用途 |
|---|---|
| Stop | 开发(快速失败) |
| Continue | 生产脚本 |
| SilentlyContinue | 预期会出现错误时 |
| 模式 | 用途 |
|---|---|
| 字面路径 | C:\Users\User\file.txt |
| 变量路径 | Join-Path $env:USERPROFILE "file.txt" |
| 相对路径 | Join-Path $ScriptDir "data" |
规则: 为跨平台安全性,使用 Join-Path。
| 操作 | 语法 |
|---|---|
| 空数组 | $array = @() |
| 添加项 | $array += $item |
| ArrayList 添加 | `$list.Add($item) |
| ❌ 错误 | ✅ 正确 |
|---|---|
ConvertTo-Json | ConvertTo-Json -Depth 10 |
规则: 对于嵌套对象,始终指定 -Depth。
| 操作 | 模式 |
|---|---|
| 读取 | `Get-Content "file.json" -Raw |
| 写入 | `$data |
| 错误信息 | 原因 | 修复方法 |
|---|---|---|
| "parameter 'or'" | 缺少括号 | 将 cmdlet 用 () 包裹 |
| "Unexpected token" | Unicode 字符 | 仅使用 ASCII |
| "Cannot find property" | 空对象 | 先检查空值 |
| "Cannot convert" | 类型不匹配 | 使用 .ToString() |
# 严格模式
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
}
记住: PowerShell 有独特的语法规则。括号、仅使用 ASCII 字符以及空值检查是不可妥协的。
每周安装量
150
代码仓库
GitHub 星标数
22.6K
首次出现
2026年1月25日
安全审计
安装于
opencode126
claude-code120
gemini-cli118
codex113
github-copilot113
cursor109
Critical patterns and pitfalls for Windows PowerShell.
| ❌ 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.
| Purpose | ❌ Don't Use | ✅ Use |
|---|---|---|
| Success | ✅ ✓ | [OK] [+] |
| Error | ❌ ✗ 🔴 | [!] [X] |
| Warning | ⚠️ 🟡 | [*] [WARN] |
| Info | ℹ️ 🔵 | [i] [INFO] |
| Progress | ⏳ | [...] |
Rule: Use ASCII characters only in PowerShell scripts.
| ❌ Wrong | ✅ Correct |
|---|---|
$array.Count -gt 0 | $array -and $array.Count -gt 0 |
$text.Length | if ($text) { $text.Length } |
| ❌ Wrong | ✅ Correct |
|---|---|
"Value: $($obj.prop.sub)" | Store in variable first |
Pattern:
$value = $obj.prop.sub
Write-Output "Value: $value"
| Value | Use |
|---|---|
| Stop | Development (fail fast) |
| Continue | Production scripts |
| SilentlyContinue | When errors expected |
| 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.
| Operation | Syntax |
|---|---|
| Empty array | $array = @() |
| Add item | $array += $item |
| ArrayList add | `$list.Add($item) |
| ❌ Wrong | ✅ Correct |
|---|---|
ConvertTo-Json | ConvertTo-Json -Depth 10 |
Rule: Always specify -Depth for nested objects.
| Operation | Pattern |
|---|---|
| Read | `Get-Content "file.json" -Raw |
| Write | `$data |
| 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() |
# 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.
Weekly Installs
150
Repository
GitHub Stars
22.6K
First Seen
Jan 25, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode126
claude-code120
gemini-cli118
codex113
github-copilot113
cursor109
Azure 升级评估与自动化工具 - 轻松迁移 Functions 计划、托管层级和 SKU
104,900 周安装
Rails应用升级分析器 - 自动化评估与升级指南
141 周安装
Skill Creator 指南:如何为 Claude AI 创建高效技能模块 | 技能开发与优化
139 周安装
CSS伪元素最佳实践与视图过渡API检查工具 - 提升前端代码质量
139 周安装
Vercel React 最佳实践指南:65条性能优化规则,提升Next.js应用性能
141 周安装
NativeWind v4 Expo 配置指南:React Native Tailwind CSS 样式库集成教程
140 周安装
AI技能创建指南 - 如何为智能体开发高效、模块化的专业技能包
139 周安装