Copilot instructions
.github/instructions/powershell-automatic-variables.instructions.mdCopilot instructions
Quality
69/100
Scores the file, not the repository.Length
719 words
33 headings · 6 code blocksRepository
55k
— · pushed 2 days agoLast changed
3 days ago
First indexed 3 days ago.1234567# PowerShell Automatic Variables - Naming Guidelines89## Purpose1011This instruction provides guidelines for avoiding conflicts with PowerShell's automatic variables when writing PowerShell scripts and modules.1213## What Are Automatic Variables?1415PowerShell has built-in automatic variables that are created and maintained by PowerShell itself. Assigning values to these variables can cause unexpected behavior and side effects.1617## Common Automatic Variables to Avoid1819### Critical Variables (Never Use)2021- **`$matches`** - Contains the results of regular expression matches. Overwriting this can break regex operations.22- **`$_`** - Represents the current object in the pipeline. Only use within pipeline blocks.23- **`$PSItem`** - Alias for `$_`. Same rules apply.24- **`$args`** - Contains an array of undeclared parameters. Don't use as a regular variable.25- **`$input`** - Contains an enumerator of all input passed to a function. Don't reassign.26- **`$LastExitCode`** - Exit code of the last native command. Don't overwrite unless intentional.27- **`$?`** - Success status of the last command. Don't use as a variable name.28- **`$$`** - Last token in the last line received by the session. Don't use.29- **`$^`** - First token in the last line received by the session. Don't use.3031### Context Variables (Use with Caution)3233- **`$Error`** - Array of error objects. Don't replace, but can modify (e.g., `$Error.Clear()`).34- **`$PSBoundParameters`** - Parameters passed to the current function. Read-only.35- **`$MyInvocation`** - Information about the current command. Read-only.36- **`$PSCmdlet`** - Cmdlet object for advanced functions. Read-only.3738### Other Common Automatic Variables3940- `$true`, `$false`, `$null` - Boolean and null constants41- `$HOME`, `$PSHome`, `$PWD` - Path-related variables42- `$PID` - Process ID of the current PowerShell session43- `$Host` - Host application object44- `$PSVersionTable` - PowerShell version information4546For a complete list, see: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_automatic_variables4748## Best Practices4950### ❌ Bad - Using Automatic Variable Names5152```powershell53# Bad: $matches is an automatic variable used for regex capture groups54$matches = Select-String -Path $file -Pattern $pattern5556# Bad: $args is an automatic variable for undeclared parameters57$args = Get-ChildItem5859# Bad: $input is an automatic variable for pipeline input60$input = Read-Host "Enter value"61```6263### ✅ Good - Using Descriptive Alternative Names6465```powershell66# Good: Use descriptive names that avoid conflicts67$matchedLines = Select-String -Path $file -Pattern $pattern6869# Good: Use specific names for arguments70$arguments = Get-ChildItem7172# Good: Use specific names for user input73$userInput = Read-Host "Enter value"74```7576## Naming Alternatives7778When you encounter a situation where you might use an automatic variable name, use these alternatives:7980| Avoid | Use Instead |81|-------|-------------|82| `$matches` | `$matchedLines`, `$matchResults`, `$regexMatches` |83| `$args` | `$arguments`, `$parameters`, `$commandArgs` |84| `$input` | `$userInput`, `$inputValue`, `$inputData` |85| `$_` (outside pipeline) | Use a named parameter or explicit variable |86| `$Error` (reassignment) | Don't reassign; use `$Error.Clear()` if needed |8788## How to Check8990### PSScriptAnalyzer Rule9192PSScriptAnalyzer has a built-in rule that detects assignments to automatic variables:9394```powershell95# This will trigger PSAvoidAssignmentToAutomaticVariable96$matches = Get-Something97```9899**Rule ID**: PSAvoidAssignmentToAutomaticVariable100101### Manual Review102103When writing PowerShell code, always:1041. Avoid variable names that match PowerShell keywords or automatic variables1052. Use descriptive, specific names that clearly indicate the variable's purpose1063. Run PSScriptAnalyzer on your code before committing1074. Review code for variable naming during PR reviews108109## Examples from the Codebase110111### Example 1: Regex Matching112113```powershell114# ❌ Bad - Overwrites automatic $matches variable115$matches = [regex]::Matches($content, $pattern)116117# ✅ Good - Uses descriptive name118$regexMatches = [regex]::Matches($content, $pattern)119```120121### Example 2: Select-String Results122123```powershell124# ❌ Bad - Conflicts with automatic $matches125$matches = Select-String -Path $file -Pattern $pattern126127# ✅ Good - Clear and specific128$matchedLines = Select-String -Path $file -Pattern $pattern129```130131### Example 3: Collecting Arguments132133```powershell134# ❌ Bad - Conflicts with automatic $args135function Process-Items {136 $args = $MyItems137 # ... process items138}139140# ✅ Good - Descriptive parameter name141function Process-Items {142 [CmdletBinding()]143 param(144 [Parameter(ValueFromRemainingArguments)]145 [string[]]$Items146 )147 # ... process items148}149```150151## References152153- [PowerShell Automatic Variables Documentation](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_automatic_variables)154- [PSScriptAnalyzer Rules](https://github.com/PowerShell/PSScriptAnalyzer/blob/master/docs/Rules/README.md)155- [PowerShell Best Practices](https://learn.microsoft.com/powershell/scripting/developer/cmdlet/strongly-encouraged-development-guidelines)156157## Summary158159**Key Takeaway**: Always use descriptive, specific variable names that clearly indicate their purpose and avoid conflicts with PowerShell's automatic variables. When in doubt, choose a longer, more descriptive name over a short one that might conflict.160
Also in PowerShell/PowerShell
Diff this repo’s formatsOne repository carrying more than one format is the comparison this product exists for: does anyone actually write different content in each file, or is one a copy of the other?
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| PowerShell/PowerShell.github/instructions/build-and-packaging-steps.instructions.md · 55k | Copilot instructions | buildagent-behaviour | 58/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/build-checkout-prerequisites.instructions.md · 55k | Copilot instructions | setupbuildstylearch+1 | 81/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/build-configuration-guide.instructions.md · 55k | Copilot instructions | buildteststyletesting-strategy+2 | 74/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/code-review-branch-strategy.instructions.md · 55k | Copilot instructions | lint-formatstyletypesgit+1 | 62/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/instruction-file-format.instructions.md · 55k | Copilot instructions | buildlint-formatstylearch+3 | 76/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/log-grouping-guidelines.instructions.md · 55k | Copilot instructions | buildtestdo-not | 77/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/onebranch-condition-syntax.instructions.md · 55k | Copilot instructions | buildstylearchdeployment+1 | 73/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/onebranch-restore-phase-pattern.instructions.md · 55k | Copilot instructions | arch | 54/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/onebranch-signing-configuration.instructions.md · 55k | Copilot instructions | buildstyledeployment | 62/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/pester-set-itresult-pattern.instructions.md · 55k | Copilot instructions | setupstylearch | 62/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/pester-test-status-and-working-meaning.instructions.md · 55k | Copilot instructions | teststyle | 54/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/powershell-module-organization.instructions.md · 55k | Copilot instructions | buildteststylearch+2 | 69/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/powershell-parameter-naming.instructions.md · 55k | Copilot instructions | styledo-not | 65/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/publishing-pester-result.instructions.md · 55k | Copilot instructions | testlint-formatstylearch+3 | 69/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/script-module-file-format.instructions.md · 55k | Copilot instructions | lint-format | 54/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/start-native-execution.instructions.md · 55k | Copilot instructions | buildstylearchgit+1 | 86/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/start-psbuild-basics.instructions.md · 55k | Copilot instructions | buildtesting-strategydeploymentagent-behaviour | 54/100 | 3 days ago | |
| PowerShell/PowerShell.github/instructions/troubleshooting-builds.instructions.md · 55k | Copilot instructions | buildgitdeployment | 54/100 | 3 days ago |
Diff against .github/instructions/build-and-packaging-steps.instructions.md Diff against .github/instructions/build-checkout-prerequisites.instructions.md Diff against .github/instructions/build-configuration-guide.instructions.md Diff against .github/instructions/code-review-branch-strategy.instructions.md Diff against .github/instructions/instruction-file-format.instructions.md Diff against .github/instructions/log-grouping-guidelines.instructions.md Diff against .github/instructions/onebranch-condition-syntax.instructions.md Diff against .github/instructions/onebranch-restore-phase-pattern.instructions.md Diff against .github/instructions/onebranch-signing-configuration.instructions.md Diff against .github/instructions/pester-set-itresult-pattern.instructions.md Diff against .github/instructions/pester-test-status-and-working-meaning.instructions.md Diff against .github/instructions/powershell-module-organization.instructions.md Diff against .github/instructions/powershell-parameter-naming.instructions.md Diff against .github/instructions/publishing-pester-result.instructions.md Diff against .github/instructions/script-module-file-format.instructions.md Diff against .github/instructions/start-native-execution.instructions.md Diff against .github/instructions/start-psbuild-basics.instructions.md Diff against .github/instructions/troubleshooting-builds.instructions.md
Similar configs
Same format, overlapping stack, ranked by quality.
| Repository | Format | Stack | Covers | Score | Changed |
|---|---|---|---|---|---|
| chihebnabil/lovable-boilerplate.github/instructions/global.instructions.md · 63 | Copilot instructions | buildlint-formatstylearch+4 | 100/100 | 3 days ago | |
| louislam/uptime-kuma.github/copilot-instructions.md · 90k | Copilot instructions | setupbuildtestlint-format+9 | 100/100 | 3 days ago | |
| pytorch/pytorch.github/copilot-instructions.md · 102k | Copilot instructions | setupbuildteststyle+5 | 100/100 | 3 days ago | |
| dotnet/roslyn.github/instructions/Compiler.instructions.md · 21k | Copilot instructions | buildteststylearch+3 | 99/100 | 3 days ago | |
| hiyouga/LlamaFactory.github/copilot-instructions.md · 74k | Copilot instructions | setupbuildtestlint-format+5 | 97/100 | 2 days ago | |
| rtk-ai/rtk.github/copilot-instructions.md · 74k | Copilot instructions | buildtestlint-formatstyle+2 | 97/100 | 3 days ago | |
| JCodesMore/ai-website-cloner-template.github/copilot-instructions.md · 31k | Copilot instructions | buildlint-formatstylearch+3 | 97/100 | 2 days ago | |
| dotnet/roslyn.github/copilot-instructions.md · 21k | Copilot instructions | buildteststylearch+3 | 97/100 | 3 days ago |
