Chuyển đến nội dung
Skillpedia
Lập trình chung48k · #5482

powershell-windows

Skill này làm gì

Hướng dẫn này chỉ bạn cách viết script PowerShell trên Windows đúng và an toàn. Nó nêu các quy tắc quan trọng để tránh lỗi thường gặp, như dùng dấu ngoặc đúng cách, tránh ký tự đặc biệt và kiểm tra xem một thứ có tồn tại trước khi dùng.

Khi nào nên dùng

  • ✓Khi bạn đang học viết script PowerShell.
  • ✓Khi script của bạn gặp lỗi bất ngờ.
  • ✓Khi muốn đảm bảo script của bạn hoạt động tốt trên các máy tính khác nhau.

Liên kết skill

https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/powershell-windows/SKILL.md

Sao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.

Cách dùng

Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.

Tin nhắn có sẵn để dán

Hãy dùng skill này để giúp tôi: https://github.com/sickn33/agentic-awesome-skills/blob/main/plugins/agentic-awesome-skills-claude/skills/powershell-windows/SKILL.md Đọc tệp SKILL.md tại liên kết đó và làm theo hướng dẫn cho việc của tôi.
Bạn muốn AI tự tìm những skill như thế này?Cài MCP Skillpedia miễn phí: chỉ một dòng, và AI sẽ tìm trong 15.000+ skill trước mỗi việc.Cài đặt trong 1 phút →

Ví dụ cách nhờ AI

“Làm thế nào để dùng dấu ngoặc đúng cách trong một script PowerShell với “or”?”

5 skill mới mỗi tuần. Đọc chỉ 1 phút.

Cách hoạt động

Hướng dẫn giải thích các quy tắc PowerShell, như việc cần dấu ngoặc trong một số tình huống và tầm quan trọng của việc chỉ dùng ký tự đơn giản (ASCII). Nó cũng chỉ cách kiểm tra một biến có giá trị hay chưa trước khi dùng.

Nội dung kỹ thuật (SKILL.md gốc)

Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.

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.
Câu hỏi thường gặp

Vì sao tôi không thể dùng emoji hoặc ký tự đặc biệt trong script?

PowerShell có thể gặp khó khăn khi hiểu các ký tự không phải tiếng Anh cơ bản (ASCII). Chỉ dùng ký tự ASCII đảm bảo script hoạt động trên mọi máy tính mà không bị lỗi lạ.

“Null checking” là gì và vì sao quan trọng?

Giống như kiểm tra xem hộp có trống không trước khi lấy đồ ra khỏi nó. Nếu bạn cố dùng thứ không tồn tại (là “null”), script có thể bị lỗi. Kiểm tra trước sẽ ngăn các lỗi này.

PowerShell xử lý lỗi như thế nào?

Bạn có thể yêu cầu PowerShell dừng mọi thứ khi có lỗi (“Stop”), tiếp tục dù có lỗi (“Continue”), hoặc bỏ qua các lỗi bạn dự đoán trước (“SilentlyContinue”). Ngoài ra còn có cách thử chạy một lệnh và bắt lỗi nếu xảy ra (“Try/Catch”).

Chi tiết

Mô tả gốc của tác giả

PowerShell Windows patterns. Critical pitfalls, operator syntax, error handling.

repo: sickn33/agentic-awesome-skills

Độ phổ biến

Sao GitHub
48k
Vị trí xếp hạng
#5482
Giấy phép
MIT
Cập nhật
15/10/2026

Skill này có hữu ích không?

0

Đăng nhập để bình chọn.

Bình luận

  • …

Thêm skill từ kho mã này

Skill tương tự