powershell-windows
它能做什么
这份指南教你如何正确、安全地编写 Windows PowerShell 脚本。它介绍了一些重要规则,帮助你避免常见错误,比如正确使用括号、避免特殊字符,以及在使用某个东西之前先检查它是否存在。
适用场景
- ✓你刚开始学习编写 PowerShell 脚本时。
- ✓脚本出现意料之外的错误时。
- ✓想确保脚本能在不同电脑上正常运行时。
技能链接
https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/powershell-windows/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“在 PowerShell 脚本里,怎样用括号正确地搭配 'or' 使用?”
每周 5 个新技能,1 分钟读完。
工作原理
指南会讲解 PowerShell 的规则,比如某些情况下需要使用括号,以及只使用简单的(ASCII)字符的重要性。它还会演示如何在使用变量之前,先检查它是否有值。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
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.
Limitations
- Use this skill only when the task clearly matches the scope described above.
- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.
- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.
常见问题
为什么脚本里不能使用表情符号或特殊字符?
PowerShell 脚本可能无法正确识别非基础英文字符(ASCII)。只使用 ASCII 字符,可以确保脚本在任何电脑上都能正常运行,不会出现奇怪的错误。
什么是“空值检查”,为什么它很重要?
就好比在从盒子里取东西之前,先看看盒子是不是空的。如果你去使用一个不存在的东西(即“null”),脚本就可能出错。先检查就能避免这类错误。
PowerShell 如何处理错误?
你可以让 PowerShell 在出错时停止所有操作('Stop'),即使出错也继续执行('Continue'),或者忽略你预料之内会出现的错误('SilentlyContinue')。另外,还可以用 'Try/Catch' 的方式尝试运行代码,并在出错时捕获错误。
详情
作者的原始描述
PowerShell Windows patterns. Critical pitfalls, operator syntax, error handling.
热度
- GitHub 星标
- 48k
- 排名
- #5482
- 许可证
- MIT
- 更新时间
- 2026/10/15
评论
- …
同一仓库的更多技能
00-andruia-consultant
48k帮你的智能体启动或改进 AI 项目。它可以分析代码、推荐工具,并用西班牙语制定行动计划。
007
48k让你的智能体执行安全审计和威胁建模。它有助于保护项目免受攻击和漏洞的威胁。
10-andruia-skill-smith
48k帮你的智能体创建或改进 AI 工具。它可以定义工具的工作方式、编写文档,并验证其结构。
20-andruia-niche-intelligence
48k让你的智能体创建某个特定行业的摘要。它有助于借助可靠的信息来源,了解项目的背景、规则、使用模式和风险。