-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Update powershell.instructions.md #1125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: staged
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -30,11 +30,11 @@ safe, and maintainable scripts. It aligns with Microsoft’s PowerShell cmdlet d | |||||
|
|
||||||
| - **Alias Avoidance:** | ||||||
| - Use full cmdlet names | ||||||
| - Avoid using aliases in scripts (e.g., use Get-ChildItem instead of gci) | ||||||
| - Avoid using aliases in scripts (e.g., use `Get-ChildItem` instead of `gci`) | ||||||
| - Document any custom aliases | ||||||
| - Use full parameter names | ||||||
|
|
||||||
| ### Example | ||||||
| ### Example - Naming Conventions | ||||||
|
|
||||||
| ```powershell | ||||||
| function Get-UserProfile { | ||||||
|
|
@@ -49,6 +49,9 @@ function Get-UserProfile { | |||||
| ) | ||||||
|
|
||||||
| process { | ||||||
| $outputString = "Searching for: '$($Username)'" | ||||||
| Write-Verbose -Message $outputString | ||||||
| Write-Verbose -Message "Profile type: $ProfileType" | ||||||
| # Logic here | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -75,12 +78,14 @@ function Get-UserProfile { | |||||
| - Enable tab completion where possible | ||||||
|
|
||||||
| - **Switch Parameters:** | ||||||
| - Use [switch] for boolean flags | ||||||
| - Avoid $true/$false parameters | ||||||
| - Default to $false when omitted | ||||||
| - Use clear action names | ||||||
| - **ALWAYS** use `[switch]` for boolean flags, never `[bool]` | ||||||
| - **NEVER** use `[bool]$Parameter` or assign default values | ||||||
| - Switch parameters default to `$false` when omitted | ||||||
| - Use clear, action-oriented names | ||||||
| - Test presence with `.IsPresent` | ||||||
| - Using `$true`/`$false` in parameter attributes (e.g., `Mandatory = $true`) is acceptable | ||||||
|
|
||||||
| ### Example | ||||||
| ### Example - Parameter Design | ||||||
|
|
||||||
| ```powershell | ||||||
| function Set-ResourceConfiguration { | ||||||
|
|
@@ -93,16 +98,24 @@ function Set-ResourceConfiguration { | |||||
| [ValidateSet('Dev', 'Test', 'Prod')] | ||||||
| [string]$Environment = 'Dev', | ||||||
|
|
||||||
| # ✔️ CORRECT: Use `[switch]` with no default value | ||||||
| [Parameter()] | ||||||
| [switch]$Force, | ||||||
|
|
||||||
| # ❌ WRONG: Shows incorrect default assignment, however this is correct syntax (requires `[switch]` cast). | ||||||
| [Parameter()] | ||||||
| [switch]$Quiet = [switch]$true, | ||||||
|
|
||||||
| [Parameter()] | ||||||
| [ValidateNotNullOrEmpty()] | ||||||
| [string[]]$Tags | ||||||
| ) | ||||||
|
|
||||||
| process { | ||||||
| # Logic here | ||||||
| # Use .IsPresent to check switch state | ||||||
| if ($Quiet.IsPresent) { | ||||||
| Write-Verbose "Quiet mode enabled" | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| ``` | ||||||
|
|
@@ -133,7 +146,7 @@ function Set-ResourceConfiguration { | |||||
| - Return modified/created object with `-PassThru` | ||||||
| - Use verbose/warning for status updates | ||||||
|
|
||||||
| ### Example | ||||||
| ### Example - Pipeline and Output | ||||||
|
|
||||||
| ```powershell | ||||||
| function Update-ResourceStatus { | ||||||
|
|
@@ -163,7 +176,7 @@ function Update-ResourceStatus { | |||||
| Name = $Name | ||||||
| Status = $Status | ||||||
| LastUpdated = $timestamp | ||||||
| UpdatedBy = $env:USERNAME | ||||||
| UpdatedBy = "$($env:USERNAME)" | ||||||
| } | ||||||
|
|
||||||
| # Only output if PassThru is specified | ||||||
|
|
@@ -209,69 +222,32 @@ function Update-ResourceStatus { | |||||
| - Support automation scenarios | ||||||
| - Document all required inputs | ||||||
|
|
||||||
| ### Example | ||||||
| ### Example - Error Handling and Safety | ||||||
|
|
||||||
| ```powershell | ||||||
| function Remove-UserAccount { | ||||||
| [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] | ||||||
| function Remove-CacheFiles { | ||||||
| [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] | ||||||
|
||||||
| [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] | |
| [CmdletBinding(SupportsShouldProcess = $true, ConfirmImpact = 'High')] |
Copilot
AI
Mar 22, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the Remove-CacheFiles example, Remove-Item is invoked without -ErrorAction Stop. Many Remove-Item failures are non-terminating by default, so the catch block may never run and the example won’t actually demonstrate the intended error handling. Consider adding -ErrorAction Stop (or setting $ErrorActionPreference = 'Stop' within the try).
| $files | Remove-Item -Force | |
| $files | Remove-Item -Force -ErrorAction Stop |
Copilot
AI
Mar 22, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$currentErrorActionValue is used to set/restore $ErrorActionPreference, but it’s never assigned. As written, the function will overwrite $ErrorActionPreference with $null (or an undefined value) and won’t restore the original preference. Store the current value first (e.g., assign $currentErrorActionValue = $ErrorActionPreference before setting it to 'Stop') and then restore it in end.
| $ErrorActionPreference = $currentErrorActionValue | |
| $currentErrorActionValue = $ErrorActionPreference |
Copilot
AI
Mar 22, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment says "ShouldProcess with Force parameter", but -Force is not used to affect ShouldProcess/confirmation (it only skips ShouldContinue). Either update the comment to reflect the actual behavior or incorporate -Force into the confirmation logic so the example matches its explanation.
Copilot
AI
Mar 22, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There’s trailing whitespace at the end of this comment line (after "had"), which can create noisy diffs later. Consider trimming it.
| # Set ErrorActionPreference back to the value it had | |
| # Set ErrorActionPreference back to the value it had |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The "WRONG" switch example assigns a default of
[switch]$truebut the function later checks$Quiet.IsPresent. If-Quietis omitted,.IsPresentwill be$falseeven though the variable value is$true, so the example’s logic becomes inconsistent/misleading. Consider removing the defaulted switch example, moving it into a separate "avoid" snippet, or updating the check to match the demonstrated pattern.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot open a new pull request to apply changes based on this feedback