Results for “Functions & parameter validation”
Small, practical examples. Search by task or narrow things down below.
Restrict a function parameter to known choices
PowerShell: ValidateSet rejects unsupported values before the function body executes.
PowerShell✓ Tested
Accept one typed value at a time from a pipeline
PowerShell: An advanced function uses process to handle each pipeline item.
PowerShell✓ Tested
Accumulate pipeline input across begin process and end
PowerShell: Keep initialization and final output separate from per-item work.
PowerShell✓ Tested
Dispatch values with wildcard patterns
PowerShell: Switch -Wildcard interprets shell-style matching patterns for each clause.
PowerShell✓ Tested
Enforce a numeric range at a function boundary
PowerShell: ValidateRange performs argument validation before work begins.
PowerShell✓ Tested
Keep function-local assignments separate from caller state
PowerShell: Ordinary function assignments create local variables rather than replacing the caller’s variable.
PowerShell✓ Tested
Pass a block of behavior as an argument
PowerShell: Invoke a scriptblock with the call operator and an explicit input value.
PowerShell✓ Tested
Preserve a failure after local cleanup
PowerShell: A bare throw inside catch rethrows the current exception.
PowerShell✓ Tested
Return a collection as one pipeline object
PowerShell: The unary comma prevents the returned array from being enumerated at this boundary.
PowerShell✓ Tested
Run cleanup after a caught exception
PowerShell: Use finally for cleanup that must occur after either success or failure.
PowerShell✓ Tested
Define a function with a typed parameter
Accept a name and return a greeting.
function✓ Tested
Pass named parameters from a hashtable
Keep a command’s options together in a reusable object.
splatting✓ Tested