So far you have used commands and written small scripts. From here on you write building blocks that others (and you, three months from now) want to use: functions that behave like the cmdlets of
PowerShell. That means they check their input, they take values from the pipeline, they return objects, they talk through -Verbose when needed, and their errors can be caught. Everything lesson PS.3.1
teaches about -WhatIf presupposes exactly this form.
All examples in this lesson were run with PowerShell 7.6.9 on Windows 11; the output shown comes from that run. PowerShell's messages appear in the system's language; here they are English.
A simple function and its limit
A function with nothing else is a named block. Parameters go in param(...):
function Get-Greeting {
param([string] $Name = 'world')
"Hello, $Name"
}
Get-Greeting -Name 'Nordlicht'
It works, but it is not yet a cmdlet: it knows no -Verbose, no -ErrorAction and no -WhatIf. The difference is a single attribute.