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
SupportsShouldProcessinCmdletBinding. - Call
$PSCmdlet.ShouldProcess(target, action)immediately before the mutating operation. - Pair with sensible
ConfirmImpactfor risk-aware prompts.
Naming
- Use approved verbs and Verb-Noun format.
- Prefer
Get-Verband approved-verb docs for validation. - Avoid synonyms of approved verbs (for example
DeletevsRemove).
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-VerboseWrite-DebugWrite-WarningWrite-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.