Mark Pitt
claude-skills
  1. claude-skills
  2. Language gurus
  3. pwsh-guru
  4. references/advanced-functions.md

A file in pwsh-guru

references/advanced-functions.md

Markdown 1.4 KB
advanced-functions.mdMarkdown, rendered

Advanced Functions

Core Pattern

  • Use [CmdletBinding()] for production-grade functions.
  • Use explicit param(...) typing and validation attributes.
  • Keep command UX consistent with built-in cmdlets.

ShouldProcess Pattern (state-changing)

  • Add SupportsShouldProcess in CmdletBinding.
  • Call $PSCmdlet.ShouldProcess(target, action) immediately before the mutating operation.
  • Pair with sensible ConfirmImpact for risk-aware prompts.

Naming

  • Use approved verbs and Verb-Noun format.
  • Prefer Get-Verb and approved-verb docs for validation.
  • Avoid synonyms of approved verbs (for example Delete vs Remove).

Parameters

  • Prefer explicit parameter names in examples and guidance.
  • Use validation attributes (ValidateSet, ValidatePattern, ValidateNotNullOrEmpty, ValidateRange) where appropriate.
  • Keep switch parameters non-mandatory unless they distinguish parameter sets.
  • Use pipeline binding only when behavior is clear and predictable.

Output Behavior

  • Output rich objects to the success stream.
  • Use stream-specific messages for diagnostics: Write-Verbose Write-Debug Write-Warning Write-Error
  • Avoid turning data output into formatted text in reusable functions.

Help

  • Add comment-based help (.SYNOPSIS, .DESCRIPTION, .PARAMETER, .EXAMPLE).
  • Keep examples executable and aligned to parameter naming conventions.