PowerShell Function Examples with Parameters

As a PowerShell developer, you should know how to use parameters in PowerShell functions. In this tutorial, I will show you how to create PowerShell functions with parameters, including both single and multiple parameters.

PowerShell function with Parameter

Let’s start with a simple example of a PowerShell function that accepts a single parameter. This can be useful when you need to pass a single piece of data to your PowerShell function.

Syntax

The basic syntax for a PowerShell function with a single parameter is like this:

function Function-Name {
    param (
        [ParameterType]$ParameterName
    )

    # Function logic here
}

Example

Let’s create a function in PowerShell that takes a name as a parameter and prints a greeting message.

function Greet-User {
    param (
        [string]$Name
    )

    Write-Output "Hello, $Name!"
}

# Calling the function
Greet-User -Name "Alice"

Here is how it works:

  1. Function Definition: The function Greet-User is defined using the function keyword.
  2. Parameter Definition: The param block is used to define parameters. Here, we define a single parameter $Name of type [string].
  3. Function Logic: The Write-Output cmdlet is used to print a greeting message that includes the value of the $Name parameter.
  4. Function Call: The function is called with the -Name parameter, passing the value "Alice".

I have executed the above PowerShell script using VS code and you can see the output in the screenshot below:

powershell function example with parameter

Read How to Handle Errors with Try-Catch in PowerShell?

PowerShell Function with Multiple Parameters

Sometimes, you need to pass multiple parameters to a PowerShell function. PowerShell makes it easy to define functions with multiple parameters.

Syntax

The syntax for a PowerShell function with multiple parameters is similar to that of a single parameter function but with multiple parameters defined within the param block.

function Function-Name {
    param (
        [ParameterType1]$ParameterName1,
        [ParameterType2]$ParameterName2
    )

    # Function logic here
}

Example

Here is a complete example to create a function that takes two parameters: a name and an age, and prints a message.

function Introduce-User {
    param (
        [string]$Name,
        [int]$Age
    )

    Write-Output "Hello, my name is $Name and I am $Age years old."
}

# Calling the function
Introduce-User -Name "Bob" -Age 30

Here is how it works:

  1. Function Definition: The function Introduce-User is defined.
  2. Parameter Definition: The param block now includes two parameters: $Name of type [string] and $Age of type [int].
  3. Function Logic: The Write-Output cmdlet prints a message that includes both the $Name and $Age parameters.
  4. Function Call: The function is called with the -Name and -Age parameters, passing the values "Bob" and 30, respectively.

You can see the output in the screenshot below after I executed the PowerShell script above.

powershell function example multiple parameters

PowerShell Function with Named Parameters

In PowerShell, Named parameters allow you to specify parameters by name when calling the function.

Here is a complete example and full PowerShell script.

function Calculate-Sum {
    param (
        [int]$Number1,
        [int]$Number2
    )

    $Sum = $Number1 + $Number2
    Write-Output "The sum of $Number1 and $Number2 is $Sum."
}

# Calling the function with named parameters
Calculate-Sum -Number1 5 -Number2 10

Read PowerShell Data Types

PowerShell Function with Positional Parameters

Positional parameters allow you to omit the parameter names when calling the function in PowerShell. The order of the arguments determines which parameter they correspond to.

function Calculate-Sum {
    param (
        [int]$Number1,
        [int]$Number2
    )

    $Sum = $Number1 + $Number2
    Write-Output "The sum of $Number1 and $Number2 is $Sum."
}

# Calling the function with positional parameters
Calculate-Sum 5 10

PowerShell Function with Switch Parameters

Switch parameters are used for boolean flags in a PowerShell function. They do not require a value; their presence alone indicates true.

function Show-Details {
    param (
        [switch]$Verbose
    )

    if ($Verbose) {
        Write-Output "Showing detailed information..."
    } else {
        Write-Output "Showing basic information..."
    }
}

# Calling the function with the switch parameter
Show-Details -Verbose

I hope you now know how to create functions with parameters in PowerShell using these examples.

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.