RuleStack

Configs

Stacks

Compare

Diff

RuleStack

Configs

Stacks

Compare

Diff

Read API

RuleStack

Configs

Stacks

Compare

Diff

Read API

Configs/Copilot instructions/PowerShell/PowerShell

Copilot instructions

.github/instructions/powershell-automatic-variables.instructions.md
Copilot instructions

Quality

69/100

Scores the file, not the repository.

Length

719 words

33 headings · 6 code blocks

Repository

55k

— · pushed 2 days ago

Last changed

3 days ago

First indexed 3 days ago.
PowerShell/PowerShell/.github/instructions/powershell-automatic-variables.instructions.mdRawGitHub
1---
2applyTo:
3 - "**/*.ps1"
4 - "**/*.psm1"
5---
6 
7# PowerShell Automatic Variables - Naming Guidelines
8 
9## Purpose
10 
11This instruction provides guidelines for avoiding conflicts with PowerShell's automatic variables when writing PowerShell scripts and modules.
12 
13## What Are Automatic Variables?
14 
15PowerShell 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.
16 
17## Common Automatic Variables to Avoid
18 
19### Critical Variables (Never Use)
20 
21- **`$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.
30 
31### Context Variables (Use with Caution)
32 
33- **`$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.
37 
38### Other Common Automatic Variables
39 
40- `$true`, `$false`, `$null` - Boolean and null constants
41- `$HOME`, `$PSHome`, `$PWD` - Path-related variables
42- `$PID` - Process ID of the current PowerShell session
43- `$Host` - Host application object
44- `$PSVersionTable` - PowerShell version information
45 
46For a complete list, see: https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_automatic_variables
47 
48## Best Practices
49 
50### ❌ Bad - Using Automatic Variable Names
51 
52```powershell
53# Bad: $matches is an automatic variable used for regex capture groups
54$matches = Select-String -Path $file -Pattern $pattern
55 
56# Bad: $args is an automatic variable for undeclared parameters
57$args = Get-ChildItem
58 
59# Bad: $input is an automatic variable for pipeline input
60$input = Read-Host "Enter value"
61```
62 
63### ✅ Good - Using Descriptive Alternative Names
64 
65```powershell
66# Good: Use descriptive names that avoid conflicts
67$matchedLines = Select-String -Path $file -Pattern $pattern
68 
69# Good: Use specific names for arguments
70$arguments = Get-ChildItem
71 
72# Good: Use specific names for user input
73$userInput = Read-Host "Enter value"
74```
75 
76## Naming Alternatives
77 
78When you encounter a situation where you might use an automatic variable name, use these alternatives:
79 
80| 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 |
87 
88## How to Check
89 
90### PSScriptAnalyzer Rule
91 
92PSScriptAnalyzer has a built-in rule that detects assignments to automatic variables:
93 
94```powershell
95# This will trigger PSAvoidAssignmentToAutomaticVariable
96$matches = Get-Something
97```
98 
99**Rule ID**: PSAvoidAssignmentToAutomaticVariable
100 
101### Manual Review
102 
103When writing PowerShell code, always:
1041. Avoid variable names that match PowerShell keywords or automatic variables
1052. Use descriptive, specific names that clearly indicate the variable's purpose
1063. Run PSScriptAnalyzer on your code before committing
1074. Review code for variable naming during PR reviews
108 
109## Examples from the Codebase
110 
111### Example 1: Regex Matching
112 
113```powershell
114# ❌ Bad - Overwrites automatic $matches variable
115$matches = [regex]::Matches($content, $pattern)
116 
117# ✅ Good - Uses descriptive name
118$regexMatches = [regex]::Matches($content, $pattern)
119```
120 
121### Example 2: Select-String Results
122 
123```powershell
124# ❌ Bad - Conflicts with automatic $matches
125$matches = Select-String -Path $file -Pattern $pattern
126 
127# ✅ Good - Clear and specific
128$matchedLines = Select-String -Path $file -Pattern $pattern
129```
130 
131### Example 3: Collecting Arguments
132 
133```powershell
134# ❌ Bad - Conflicts with automatic $args
135function Process-Items {
136 $args = $MyItems
137 # ... process items
138}
139 
140# ✅ Good - Descriptive parameter name
141function Process-Items {
142 [CmdletBinding()]
143 param(
144 [Parameter(ValueFromRemainingArguments)]
145 [string[]]$Items
146 )
147 # ... process items
148}
149```
150 
151## References
152 
153- [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)
156 
157## Summary
158 
159**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 

Sections

  • PowerShell Automatic Variables - Naming Guidelines
  • Purpose
  • What Are Automatic Variables?
  • Common Automatic Variables to Avoid
  • Critical Variables (Never Use)
  • Context Variables (Use with Caution)
  • Other Common Automatic Variables
  • Best Practices
  • ❌ Bad - Using Automatic Variable Names
  • Bad: $matches is an automatic variable used for regex capture groups
  • Bad: $args is an automatic variable for undeclared parameters
  • Bad: $input is an automatic variable for pipeline input
  • ✅ Good - Using Descriptive Alternative Names
  • Good: Use descriptive names that avoid conflicts
  • Good: Use specific names for arguments
  • Good: Use specific names for user input
  • Naming Alternatives
  • How to Check
  • PSScriptAnalyzer Rule
  • This will trigger PSAvoidAssignmentToAutomaticVariable
  • Manual Review
  • Examples from the Codebase
  • Example 1: Regex Matching
  • ❌ Bad - Overwrites automatic $matches variable
  • ✅ Good - Uses descriptive name
  • Example 2: Select-String Results
  • ❌ Bad - Conflicts with automatic $matches
  • ✅ Good - Clear and specific
  • Example 3: Collecting Arguments
  • ❌ Bad - Conflicts with automatic $args
  • ✅ Good - Descriptive parameter name
  • References
  • Summary

What it covers

code-stylearchitecturegit-prdeploymentdo-not

Stack — with the evidence

csharp

(1.00)

dotnet

(1.00)

github-actions

(0.60)

Glob targeting

  • **/*.ps1
  • **/*.psm1

Format

Copilot instructions

Two layers: one always-on repo file, plus optional glob-scoped instruction files. Lives under .github/ rather than the repo root, which is the tell that it is aimed at the GitHub platform surface as much as the editor.

What the corpus says about it

Repository

Owner
PowerShell
Language
—
License
—
Archived
no

All configs in this repo

Also in PowerShell/PowerShell

Diff this repo’s formats

One 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?

The other instruction files in this repository
RepositoryFormatStackCoversScoreChanged
PowerShell/PowerShell.github/instructions/build-and-packaging-steps.instructions.md · 55kCopilot instructionscsharpdotnet+1buildagent-behaviour58/1003 days ago
PowerShell/PowerShell.github/instructions/build-checkout-prerequisites.instructions.md · 55kCopilot instructionscsharpdotnet+1setupbuildstylearch+181/1003 days ago
PowerShell/PowerShell.github/instructions/build-configuration-guide.instructions.md · 55kCopilot instructionscsharpdotnet+1buildteststyletesting-strategy+274/1003 days ago
PowerShell/PowerShell.github/instructions/code-review-branch-strategy.instructions.md · 55kCopilot instructionscsharpdotnet+1lint-formatstyletypesgit+162/1003 days ago
PowerShell/PowerShell.github/instructions/instruction-file-format.instructions.md · 55kCopilot instructionscsharpdotnet+1buildlint-formatstylearch+376/1003 days ago
PowerShell/PowerShell.github/instructions/log-grouping-guidelines.instructions.md · 55kCopilot instructionscsharpdotnet+1buildtestdo-not77/1003 days ago
PowerShell/PowerShell.github/instructions/onebranch-condition-syntax.instructions.md · 55kCopilot instructionscsharpdotnet+1buildstylearchdeployment+173/1003 days ago
PowerShell/PowerShell.github/instructions/onebranch-restore-phase-pattern.instructions.md · 55kCopilot instructionscsharpdotnet+1arch54/1003 days ago
PowerShell/PowerShell.github/instructions/onebranch-signing-configuration.instructions.md · 55kCopilot instructionscsharpdotnet+1buildstyledeployment62/1003 days ago
PowerShell/PowerShell.github/instructions/pester-set-itresult-pattern.instructions.md · 55kCopilot instructionscsharpdotnet+1setupstylearch62/1003 days ago
PowerShell/PowerShell.github/instructions/pester-test-status-and-working-meaning.instructions.md · 55kCopilot instructionscsharpdotnet+1teststyle54/1003 days ago
PowerShell/PowerShell.github/instructions/powershell-module-organization.instructions.md · 55kCopilot instructionscsharpdotnet+1buildteststylearch+269/1003 days ago
PowerShell/PowerShell.github/instructions/powershell-parameter-naming.instructions.md · 55kCopilot instructionscsharpdotnet+1styledo-not65/1003 days ago
PowerShell/PowerShell.github/instructions/publishing-pester-result.instructions.md · 55kCopilot instructionscsharpdotnet+1testlint-formatstylearch+369/1003 days ago
PowerShell/PowerShell.github/instructions/script-module-file-format.instructions.md · 55kCopilot instructionscsharpdotnet+1lint-format54/1003 days ago
PowerShell/PowerShell.github/instructions/start-native-execution.instructions.md · 55kCopilot instructionscsharpdotnet+1buildstylearchgit+186/1003 days ago
PowerShell/PowerShell.github/instructions/start-psbuild-basics.instructions.md · 55kCopilot instructionscsharpdotnet+1buildtesting-strategydeploymentagent-behaviour54/1003 days ago
PowerShell/PowerShell.github/instructions/troubleshooting-builds.instructions.md · 55kCopilot instructionscsharpdotnet+1buildgitdeployment54/1003 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.

Same format, overlapping stack, ranked by quality
RepositoryFormatStackCoversScoreChanged
chihebnabil/lovable-boilerplate.github/instructions/global.instructions.md · 63Copilot instructionstypescriptreact+7buildlint-formatstylearch+4100/1003 days ago
louislam/uptime-kuma.github/copilot-instructions.md · 90kCopilot instructionstypescriptjavascript+10setupbuildtestlint-format+9100/1003 days ago
pytorch/pytorch.github/copilot-instructions.md · 102kCopilot instructionspythonpytorch+4setupbuildteststyle+5100/1003 days ago
dotnet/roslyn.github/instructions/Compiler.instructions.md · 21kCopilot instructionscsharpdotnet+1buildteststylearch+399/1003 days ago
hiyouga/LlamaFactory.github/copilot-instructions.md · 74kCopilot instructionspythontransformers+4setupbuildtestlint-format+597/1002 days ago
rtk-ai/rtk.github/copilot-instructions.md · 74kCopilot instructionsrustgithub-actionsbuildtestlint-formatstyle+297/1003 days ago
JCodesMore/ai-website-cloner-template.github/copilot-instructions.md · 31kCopilot instructionstypescriptnode+7buildlint-formatstylearch+397/1002 days ago
dotnet/roslyn.github/copilot-instructions.md · 21kCopilot instructionscsharpdotnet+1buildteststylearch+397/1003 days ago
RuleStack

Built by

Kynth Studio

Directory

Configs
Stacks
Compare formats
Diff two configs
Best AGENTS.md examples

Formats

AGENTS.md
CLAUDE.md
Cursor rules
Copilot instructions

Reference

Read API
Corpus health
Privacy Policy
Terms

RuleStack

RuleStack

Built by

Kynth Studio

Directory

Configs
Stacks
Compare formats
Diff two configs
Best AGENTS.md examples

Formats

AGENTS.md
CLAUDE.md
Cursor rules
Copilot instructions

Reference

Read API
Corpus health
Privacy Policy
Terms

RuleStack

RuleStack

Built by

Kynth Studio

Directory

Configs
Stacks
Compare formats
Diff two configs
Best AGENTS.md examples

Formats

AGENTS.md
CLAUDE.md
Cursor rules
Copilot instructions

Reference

Read API
Corpus health
Privacy Policy
Terms

RuleStack