# PowerShell Functions

A function is basically a named block of code. When you call the function name, the script block within that function runs. It is a list of PowerShell statements that has a name that you assign. When you run a function, you type the function name.It is a method of saving time when tackling repetitive tasks. PowerShell formats in three parts: the keyword 'Function', followed by a Name, finally, the payload containing the script block, which is enclosed by curly/parenthesis style bracket.

# Basic Parameters

A function can be defined with parameters using the param block:

function Write-Greeting {
    "Hello $name, you are $age years old."

Or using the simple function syntax:

function Write-Greeting ($name, $age) {
    "Hello $name, you are $age years old."

Note: Casting parameters is not required in either type of parameter definition.

Simple function syntax (SFS) has very limited capabilities in comparison to the param block.
Though you can define parameters to be exposed within the function, you cannot specify Parameter Attributes, utilize Parameter Validation, include [CmdletBinding()], with SFS (and this is a non-exhaustive list).

Functions can be invoked with ordered or named parameters.

The order of the parameters on the invocation is matched to the order of the declaration in the function header (by default), or can be specified using the Position Parameter Attribute (as shown in the advanced function example, above).

$greeting = Write-Greeting "Jim" 82

Alternatively, this function can be invoked with named parameters

$greeting = Write-Greeting -name "Bob" -age 82

# Advanced Function

This is a copy of the advanced function snippet from the Powershell ISE. Basically this is a template for many of the things you can use with advanced functions in Powershell. Key points to note:

  • get-help integration - the beginning of the function contains a comment block that is set up to be read by the get-help cmdlet. The function block may be located at the end, if desired.
  • cmdletbinding - function will behave like a cmdlet
  • parameters
  • parameter sets
   Short description
   Long description
   Example of how to use this cmdlet
   Another example of how to use this cmdlet
   Inputs to this cmdlet (if any)
   Output from this cmdlet (if any)
   General notes
   The component this cmdlet belongs to
   The role this cmdlet belongs to
   The functionality that best describes this cmdlet
function Verb-Noun
    [CmdletBinding(DefaultParameterSetName='Parameter Set 1', 
                  HelpUri = 'http://www.microsoft.com/',
        # Param1 help description
                   ParameterSetName='Parameter Set 1')]
        [ValidateSet("sun", "moon", "earth")]

        # Param2 help description
        [Parameter(ParameterSetName='Parameter Set 1')]

        # Param3 help description
        [Parameter(ParameterSetName='Another Parameter Set')]

        if ($pscmdlet.ShouldProcess("Target", "Operation"))

# Mandatory Parameters

Parameters to a function can be marked as mandatory

function Get-Greeting{
    "Hello World $name"

If the function is invoked without a value, the command line will prompt for the value:

$greeting = Get-Greeting 

cmdlet Get-Greeting at command pipeline position 1
Supply values for the following parameters:

# Parameter Validation

There are a variety of ways to validate parameter entry, in PowerShell.

Instead of writing code within functions or scripts to validate parameter values, these ParameterAttributes will throw if invalid values are passed.

# ValidateSet

Sometimes we need to restrict the possible values that a parameter can accept. Say we want to allow only red, green and blue for the $Color parameter in a script or function.

We can use the ValidateSet parameter attribute to restrict this. It has the additional benefit of allowing tab completion when setting this argument (in some environments).


You can also specify IgnoreCase to disable case sensitivity.

# ValidateRange

This method of parameter validation takes a min and max Int32 value, and requires the parameter to be within that range.


# ValidatePattern

This method of parameter validation accepts parameters that match the regex pattern specified.


# ValidateLength

This method of parameter validation tests the length of the passed string.


# ValidateCount

This method of parameter validation tests the amount of arguments passed in, for example, an array of strings.


# ValidateScript

Finally, the ValidateScript method is extraordinarily flexible, taking a scriptblock and evaluating it using $_ to represent the passed argument. It then passes the argument if the result is $true (including any output as valid).

This can be used to test that a file exists:

    [ValidateScript({Test-Path $_})]

To check that a user exists in AD:

    [ValidateScript({Get-ADUser $_})]

And pretty much anything else you can write (as it's not restricted to oneliners):

        $AnHourAgo = (Get-Date).AddHours(-1)
        if ($_ -lt $AnHourAgo.AddMinutes(5) -and $_ -gt $AnHourAgo.AddMinutes(-5)) {
        } else {
            throw "That's not within five minutes. Try again."

# Simple Function with No Parameters

This is an example of a function which returns a string. In the example, the function is called in a statement assigning a value to a variable. The value in this case is the return value of the function.

function Get-Greeting{
    "Hello World"

# Invoking the function
$greeting = Get-Greeting

# demonstrate output

function declares the following code to be a function.

Get-Greeting is the name of the function. Any time that function needs to be used in the script, the function can be called by means of invoking it by name.

{ ... } is the script block that is executed by the function.

If the above code is executed in the ISE, the results would be something like:

Hello World
Hello World