Search notes:

PowerShell: functions

A function is one of the PowerShell command types which have an entry in the System.Management.Automation.CommandTypes enum.

A function is a script block

A function is basically a named script block:
PS C:\> function dummy() { write-output 42 }
PS C:\> $function:dummy.GetType().FullName
System.Management.Automation.ScriptBlock

All output goes to the pipeline

All output that a function produces goes to the pipeline.
This is fundamentally different from class methods where only values go to the pipeline that are explicitly mentioned with the return statement.

Turning a function into a cmdlet

A function can be turned into a cmdLet by adding [cmdletBinding[()] param() to its declaration. Such a function is said to be advanced:
function do-something {
   [cmdletBinding()]
    param (
      … 
    )
    …
}

Displaying the definition of function

The definition of function might be displayed with the get-content cmdlet:
PS C:\> get-content function:/mkdir

See also

Advanced functions
The .NET classes System.Management.Automation.FunctionInfo and .NET class System.Management.Automation.CmdletBindingAttribute.
The existence of a function can be verified with
PS C:\Users\Rene> test-path function:\mkdir
True
Functions exist in a scope.
Providers
Create a global variable in a function.
A filter is a function that just has the process block.

Index