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.
Functions
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.
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.
function defined → parameters bound → logic runs → objects returned
Test boundary/error cases and independently verify external target state.
Continue only after interpreting the current observation.Context & applicabilityCommon mistakes · platform/version boundaries
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.
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
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 · 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.
- 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.
SupportsShouldProcessenables a control contract but still requires the function to call ShouldProcess around state-changing actions.
$PSVersionTable— PowerShell edition and versionGet-Command <name>— which cmdlet/function/exe runsGet-Help <cmdlet> -Full— syntax and parametersGet-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.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
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%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.
Quick lookup tables
PowerShell → Functions reference map
Fast lookup boundaries for PowerShell → Functions.
| Item | Meaning | Evidence boundary |
|---|---|---|
| Parameters | Input contract | binding success does not prove valid external state |
| Begin | Once-before pipeline | not per-item processing |
| Process | Per pipeline item | cardinality/error behavior must be defined |
| End | Finalization | can run after many partial results |
| Output stream | Caller data contract | accidental output can break consumers |