Pcornel
Browse topics
Options
Recent
Favorites
Windows · Technical reference

Functions

Direct 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.

When to use this

Use this topic when designing reusable automation, defining clear parameters/output, accepting pipeline input, separating initialization/per-item/finalization work, or debugging scope and unintended output.

First 30 seconds

1. Get-Command <func> — loaded?

2. Use param() with types and [CmdletBinding()].

3. Return objects (not Write-Host).

4. Test: call with known input and -Verbose.

5. Errors: throw / Write-Error, handled by callers.

Operational verification

function defined → parameters bound → logic runs → objects returned

Evidence · next step

Test boundary/error cases and independently verify external target state.

Continue only after interpreting the current observation.
Context & applicabilityCommon mistakes · platform/version boundaries
Common mistakes
Common traps

Do not use Write-Host as a data return mechanism. Avoid leaking helper-command output unintentionally. Do not hide required state in globals. If a function sometimes emits strings and sometimes domain objects, downstream pipelines become fragile.

Platform / version applicability
Windows execution, automation and observability
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformWindows execution, automation and observability
Scope / prerequisiteUse this topic when designing reusable automation, defining clear parameters/output, accepting pipeline input, separati…
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

1 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 Verification path Verify boundary

Test parameter validation, zero/one/many pipeline inputs, error cases and output type. Use Pester or equivalent automated tests for stable contracts. For state-changing functions, validate inputs/preconditions, use ShouldProcess where appropriate and perform a separate target check.

Evidence references

Evidence · 1 on this Topic

Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.

observation · 1
Key points
  • 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.
Key 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
ReferenceWhat it is1 section · depth on demand

Core model

Well-designed PowerShell functions make dependencies visible through parameters and return structured objects rather than formatted text. Advanced functions bring cmdlet-like features such as common parameters, validation attributes and pipeline binding.

ReferenceImportant terms & boundaries2 sections · depth on demand

PowerShell boundary

Use Begin for initialization, Process for each pipeline item and End for finalization/aggregation. Be deliberate about error behavior and partial results. A function should not silently depend on a caller profile/global variable if it is intended for automation.

In depth

Advanced functions behave like cmdlets: parameter validation, pipeline input, -WhatIf support.

function Get-DiskFree {
    [CmdletBinding()]
    param(
        [Parameter(ValueFromPipeline)][string[]]$ComputerName = $env:COMPUTERNAME,
        [ValidateRange(1,100)][int]$WarnPercent = 15
    )
    process {
        foreach ($c in $ComputerName) {
            Get-CimInstance Win32_LogicalDisk -ComputerName $c -Filter 'DriveType=3' |
                Select-Object @{n='Computer';e={$c}},DeviceID,@{n='FreePct';e={[math]::Round($_.FreeSpace/$_.Size*100,1)}} |
                Where-Object FreePct -lt $WarnPercent
        }
    }
}
'srv01','srv02' | Get-DiskFree -WarnPercent 20

Output objects, not text, so callers can sort, filter and export.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docWindows execution, automation and observability current

Authoritative/upstream documentation reviewed for bounded claims in PowerShell → Functions. Documentation verification only; this is not observed lab or production Evidence.

Basis: https://learn.microsoft.com/en-us/powershell/ · confidence 94%
Terminology layer

Terms & aliases

Hover a linked term for a quick definition; click it to open its glossary entry. Aliases are other names that find this Topic in search.

Aliases that resolve here
powershell functionsadvanced functions powershellcmdletbinding powershellpowershell begin process end
Embedded reference data

Quick lookup tables

PowerShell → Functions reference map

Fast lookup boundaries for PowerShell → Functions.

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
Confirmation

Confirm action

Continue?