[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill-de2b9d1a-426d-46b7-a1d1-8ee1405aa024":3,"$fiOkwHdBOAF2Ko_-3Ro8jva_9v7rospoL89kkeOv841Q":43},{"id":4,"title":5,"description":6,"categoryId":7,"moduleId":8,"tags":9,"prompt":10,"icon":11,"source":12,"sourceUrl":13,"authorId":14,"authorName":15,"isPublic":16,"stars":17,"runs":18,"createdAt":19,"updatedAt":19,"module":20,"category":27,"packages":34},"de2b9d1a-426d-46b7-a1d1-8ee1405aa024","windows-shell-reliability","Windows上可靠的命令执行：路径、编码和常见二进制陷阱。","cat_life_career","mod_other","sickn33,other","---\nname: windows-shell-reliability\ndescription: \"Reliable command execution on Windows: paths, encoding, and common binary pitfalls.\"\nrisk: safe\nsource: community\ndate_added: \"2026-03-19\"\n---\n\n# Windows Shell Reliability Patterns\n\n> Best practices for running commands on Windows via PowerShell and CMD.\n\n## When to Use\nUse this skill when developing or debugging scripts and automation that run on Windows systems, especially when involving file paths, character encoding, or standard CLI tools.\n\n---\n\n## 1. Encoding & Redirection\n\n### CRITICAL: Redirection Differences Across PowerShell Versions\nOlder Windows PowerShell releases can rewrite native-command output in ways that break\nlater processing. PowerShell 7.4+ preserves the byte stream when redirecting stdout,\nso only apply the UTF-8 conversion workaround when you are dealing with older shell\nbehavior or a log file that is already unreadable.\n\n| Problem | Symptom | Solution |\n|---------|---------|----------|\n| `dotnet > log.txt` | `view_file` fails in older Windows PowerShell | `Get-Content log.txt | Set-Content -Encoding utf8 log_utf8.txt` |\n| `npm run > log.txt` | Need a UTF-8 text log with errors included | `npm run ... 2>&1 | Out-File -Encoding UTF8 log.txt` |\n\n**Rule:** Prefer native redirection as-is on PowerShell 7.4+, and use explicit UTF-8\nconversion only when older Windows PowerShell redirection produces an unreadable log.\n\n---\n\n## 2. Handling Paths & Spaces\n\n### CRITICAL: Quoting\nWindows paths often contain spaces.\n\n| ❌ Wrong | ✅ Correct |\n|----------|-----------|\n| `dotnet build src\u002Fmy project\u002Ffile.fsproj` | `dotnet build \"src\u002Fmy project\u002Ffile.fsproj\"` |\n| `& C:\\Path With Spaces\\bin.exe` | `& \"C:\\Path With Spaces\\bin.exe\"` |\n\n**Rule:** Always quote absolute and relative paths that may contain spaces.\n\n### The Call Operator (&)\nIn PowerShell, if an executable path starts with a quote, you MUST use the `&` operator.\n\n**Pattern:**\n```powershell\n& \"C:\\Program Files\\dotnet\\dotnet.exe\" build ...\n```\n\n---\n\n## 3. Common Binary & Cmdlet Pitfalls\n\n| Action | ❌ CMD Style | ✅ PowerShell Choice |\n|--------|-------------|---------------------|\n| Delete | `del \u002Ff \u002Fq file` | `Remove-Item -Force file` |\n| Copy | `copy a b` | `Copy-Item a b` |\n| Move | `move a b` | `Move-Item a b` |\n| Make Dir | `mkdir folder` | `New-Item -ItemType Directory -Path folder` |\n\n**Tip:** Using CLI aliases like `ls`, `cat`, and `cp` in PowerShell is usually fine, but using full cmdlets in scripts is more robust.\n\n---\n\n## 4. Dotnet CLI Reliability\n\n### Build Speed & Consistency\n| Context | Command | Why |\n|---------|---------|-----|\n| Fast Iteration | `dotnet build --no-restore` | Skips redundant nuget restore. |\n| Clean Build | `dotnet build --no-incremental` | Ensures no stale artifacts. |\n| Background | `Start-Process dotnet -ArgumentList 'run' -RedirectStandardOutput output.txt -RedirectStandardError error.txt` | Launches the app without blocking the shell and keeps logs. |\n\n---\n\n## 5. Environment Variables\n\n| Shell | Syntax |\n|-------|--------|\n| PowerShell | `$env:VARIABLE_NAME` |\n| CMD | `%VARIABLE_NAME%` |\n\n---\n\n## 6. Long Paths\nWindows has a 260-character path limit by default.\n\n**Fix:** If you hit long path errors, use the extended path prefix:\n`\\\\?\\C:\\Very\\Long\\Path\\...`\n\n---\n\n## 7. Troubleshooting Shell Errors\n\n| Error | Likely Cause | Fix |\n|-------|-------------|-----|\n| `The term 'xxx' is not recognized` | Path not in $env:PATH | Use absolute path or fix PATH. |\n| `Access to the path is denied` | File in use or permissions | Stop process or run as Admin. |\n| `Encoding mismatch` | Older shell redirection rewrote the output | Re-export the file as UTF-8 or capture with `2>&1 | Out-File -Encoding UTF8`. |\n\n---\n\n## Limitations\n- Use this skill only when the task clearly matches the scope described above.\n- Do not treat the output as a substitute for environment-specific validation, testing, or expert review.\n- Stop and ask for clarification if required inputs, permissions, safety boundaries, or success criteria are missing.\n","","imported","https:\u002F\u002Fgithub.com\u002Fsickn33\u002Fantigravity-awesome-skills","user_system_seed","SkillOPIC",true,115,705,"2026-05-16 13:47:20",{"id":8,"name":21,"slug":22,"icon":23,"description":24,"sort":25,"createdAt":26},"其他","other","mdi-page-next-outline","其他类型Skill",5,"2026-05-16 12:53:40",{"id":7,"name":28,"slug":29,"icon":30,"description":31,"moduleId":8,"sort":32,"skillCount":33,"createdAt":26},"职场发展","career","mdi-briefcase-outline","面试准备、简历优化、职业规划",4,575,[35],{"id":36,"skillId":4,"version":37,"fileName":38,"fileSize":39,"filePath":40,"fileHash":41,"manifest":42,"createdAt":19},"e1bc0f50-9c6c-49bd-b8f7-38aa86901d54","1.0.0","windows-shell-reliability.zip",2040,"uploads\u002Fskills\u002Fde2b9d1a-426d-46b7-a1d1-8ee1405aa024\u002Fwindows-shell-reliability.zip","daee1817570063de6f6c09e66d164d2616c3e4f5a3ae4df33194e021ac041108","[{\"path\":\"SKILL.md\",\"isDirectory\":false,\"size\":4038}]",{"code":44,"message":45,"data":46},200,"success",{"items":47,"stats":48,"page":51},[],{"averageRating":49,"totalRatings":49,"ratingCounts":50},0,[49,49,49,49,49],{"limit":52,"offset":49,"hasMore":53,"nextOffset":52,"ratedOnly":16},15,false]