Pcornel
Windows · PowerShell

Functions

Cheat sheet · https://pcornel.dk/topic.php?topic=3045

Back to the Topic

Answer

PowerShell functions package reusable logic. Advanced functions use CmdletBinding-style metadata so they can behave more like cmdlets with parameter binding, common parameters, pipeline support and optionally ShouldProcess.

Remember

  • Anything written to the success output stream can become function output, including accidental expressions.
  • Advanced functions can expose common parameters and cmdlet-like parameter metadata.
  • Pipeline-aware functions should distinguish Begin/Process/End behavior and cardinality.
  • Function-local scope protects variables unless broader scopes are explicitly targeted.
  • SupportsShouldProcess enables a control contract but still requires the function to call ShouldProcess around state-changing actions.

First checks

  • $PSVersionTable — PowerShell edition and version
  • Get-Command <name> — which cmdlet/function/exe runs
  • Get-Help <cmdlet> -Full — syntax and parameters
  • Get-ExecutionPolicy -List — execution policy per scope

Verify in this order

function defined → parameters bound → logic runs → objects returned

Where it usually breaks

First mismatch in definition/configuration → security context → service/task/process creation → runtime state → output/side effect → event/performance evidence; preserve identity/native error/pre-state before mutation.

PowerShell → Functions reference map

ItemMeaningEvidence boundary
ParametersInput contractbinding success does not prove valid external state
BeginOnce-before pipelinenot per-item processing
ProcessPer pipeline itemcardinality/error behavior must be defined
EndFinalizationcan run after many partial results
Output streamCaller data contractaccidental output can break consumers

Read-only verification commands unless stated otherwise. Confirm syntax for your platform and release. Full reference: https://pcornel.dk/topic.php?topic=3045

Confirmation

Confirm action

Continue?