Skip to content

Conversation

time-by-waves
Copy link
Contributor

Update error handling examples to use $PSCmdlet.WriteError() and $PSCmdlet.ThrowTerminatingError() instead of Write-Error and throw for better PowerShell cmdlet integration.

Pull Request Checklist

  • I have read and followed the CONTRIBUTING.md guidelines.
  • My contribution adds a new instruction, prompt, or chat mode file in the correct directory.
  • The file follows the required naming convention.
  • The content is clearly structured and follows the example format.
  • I have tested my instructions, prompt, or chat mode with GitHub Copilot.
  • I have run node update-readme.js and verified that README.md is up to date.

Description

Updates the error handling section and examples in the PowerShell cmdlet development guidelines to follow best practices for advanced functions.

Changes:

  • Updated error handling guidelines to recommend $PSCmdlet methods over Write-Error and throw in advanced functions
  • Added guidance on constructing proper ErrorRecord objects with error IDs, categories, and target objects
  • Updated Remove-UserAccount example to demonstrate $PSCmdlet.WriteError() for non-terminating errors and $PSCmdlet.ThrowTerminatingError() for terminating errors
  • Updated New-Resource example to use $PSCmdlet.ThrowTerminatingError() with proper ErrorRecord construction

These changes ensure GitHub Copilot generates PowerShell code with better error reporting and integration with PowerShell's error handling pipeline.


Type of Contribution

  • New instruction file.
  • New prompt file.
  • New chat mode file.
  • Other (please specify): Update to existing instruction file

Additional Notes

This update improves the existing PowerShell guidelines (example.md) to align more closely with Microsoft's recommended patterns for advanced function error handling. The use of $PSCmdlet.WriteError() and $PSCmdlet.ThrowTerminatingError() provides better integration with PowerShell's error handling pipeline and allows for more detailed error categorization.


By submitting this pull request, I confirm that my contribution abides by the Code of Conduct and will be licensed under the MIT License.

Update error handling examples to use $PSCmdlet.WriteError() and
$PSCmdlet.ThrowTerminatingError() instead of Write-Error and throw for better PowerShell cmdlet integration.
@Copilot Copilot AI review requested due to automatic review settings October 2, 2025 18:37
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves error handling practices in PowerShell cmdlet development guidelines by transitioning from basic PowerShell error handling methods to advanced function-specific approaches using $PSCmdlet methods.

  • Updated error handling guidelines to recommend $PSCmdlet.WriteError() and $PSCmdlet.ThrowTerminatingError() over Write-Error and throw
  • Enhanced examples with proper ErrorRecord construction including error IDs, categories, and target objects
  • Standardized string quotes from double to single quotes throughout examples

begin {
Write-Verbose "Starting resource status update process"
Write-Verbose 'Starting resource status update process'
Copy link

Copilot AI Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Inconsistent quote style change. The original double quotes were appropriate and follow PowerShell conventions for strings that may contain variables or escape sequences. Consider keeping double quotes for consistency with existing PowerShell practices.

Suggested change
Write-Verbose 'Starting resource status update process'
Write-Verbose "Starting resource status update process"

Copilot uses AI. Check for mistakes.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, expandable strings are only necessary when they contain variables or subexpressions. Otherwise, string literals are perfectly fine, and PSSA even has a rule (off by default) to convert any string literals to single quotes: PSAvoidUsingDoubleQuotesForConstantString

# Only output if PassThru is specified
if ($PassThru) {
if ($PassThru.IsPresent) {
Copy link

Copilot AI Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change from if ($PassThru) to if ($PassThru.IsPresent) is unnecessary. PowerShell switch parameters evaluate to $true when present and $false when absent, making the .IsPresent property redundant in boolean contexts.

Suggested change
if ($PassThru.IsPresent) {
if ($PassThru) {

Copilot uses AI. Check for mistakes.

Comment on lines +235 to +241
$errorRecord = [System.Management.Automation.ErrorRecord]::new(
[System.Exception]::new("User account '$Username' not found"),
'UserNotFound',
[System.Management.Automation.ErrorCategory]::ObjectNotFound,
$Username
)
$PSCmdlet.WriteError($errorRecord)
Copy link

Copilot AI Oct 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Good improvement using $PSCmdlet.WriteError() with proper ErrorRecord construction. However, consider using a more specific exception type like [System.ArgumentException] instead of the generic [System.Exception] for better error categorization.

Copilot uses AI. Check for mistakes.

Copy link

@TMA-2 TMA-2 Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering it categorizes it as ObjectNotFound, I'd say it's perfectly fine, unless you have a better .NET exception it could use? Perhaps [Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException]?

@aaronpowell aaronpowell merged commit 897d61b into github:main Oct 8, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants