PowerShell Functions: Return Values and Multiple Values

This tutorial explains how to work with PowerShell functions that return values, both single and multiple. I will provide examples and show you how to return multiple values from a PowerShell function.

PowerShell function example return value

If you are new to functions in PowerShell, then here is the syntax for declaring a function.

function Get-Greeting {
    param (
        [string]$Name
    )
    "Hello, $Name!"
}

In this example, the function Get-Greeting takes a single parameter $Name and returns a greeting string.

Return a Single Value from a PowerShell function

In PowerShell, you can return a value from a function either by using the return keyword or simply by outputting the value. Here’s how you can do it:

function Get-Square {
    param (
        [int]$Number
    )
    return $Number * $Number
}

# Usage
$result = Get-Square -Number 5
Write-Output $result

In this example, the function Get-Square calculates the square of the input number and returns it using the return keyword.

You can see the output in the screenshot below after executing the script above using VS code.

powershell function example return value

Alternatively, you can output the value directly without using return:

function Get-Square {
    param (
        [int]$Number
    )
    $Number * $Number
}

# Usage
$result = Get-Square -Number 5
Write-Output $result  # Output: 25

Both methods are valid, but using the return keyword can make it clearer that the function is intended to return a value.

Read PowerShell Function Examples with Parameters

Return Multiple Values from a Function

Using various methods, you can also return multiple values from a function in PowerShell.

Using an Array

One way to return multiple values from a PowerShell function is to use an array. Here is an example:

function Get-Coordinates {
    param (
        [int]$X,
        [int]$Y
    )
    return @($X, $Y)
}

# Usage
$coords = Get-Coordinates -X 10 -Y 20
Write-Output "X: $($coords[0]), Y: $($coords[1])"

In this example, the function Get-Coordinates returns an array containing the X and Y coordinates.

You can see the output in the screenshot below:

powershell function return multiple values

Using a Hashtable

You can also return multiple values from an array using a hashtable, which allows you to return named values. Here is an example:

function Get-UserInfo {
    param (
        [string]$Name,
        [int]$Age
    )
    return @{
        Name = $Name
        Age  = $Age
    }
}

# Usage
$userInfo = Get-UserInfo -Name "Alice" -Age 30
Write-Output "Name: $($userInfo.Name), Age: $($userInfo.Age)"  # Output: Name: Alice, Age: 30

Here, the function Get-UserInfo returns a hashtable with the user’s name and age.

Using a Custom Object

From a PowerShell function, you can also return multiple values by using a custom object. Here is an example:

function Get-Employee {
    param (
        [string]$Name,
        [string]$Title
    )
    $employee = [PSCustomObject]@{
        Name  = $Name
        Title = $Title
    }
    return $employee
}

# Usage
$employee = Get-Employee -Name "John Doe" -Title "Manager"
Write-Output "Name: $($employee.Name), Title: $($employee.Title)"  # Output: Name: John Doe, Title: Manager

In this example, Get-Employee returns a custom object with properties Name and Title.

Read Try-Catch in PowerShell

PowerShell Functions: Using Parameters and Returning Values

In this section, I will show a few PowerShell function examples with parameters and return values.

Like, I will show you now how to define functions with parameters and return values.

PowerShell Function with Parameters

To define a function with parameters in PowerShell, you use the param block. Here’s a simple example:

function Get-Greeting {
    param (
        [string]$Name
    )
    "Hello, $Name!"
}

# Usage
$greeting = Get-Greeting -Name "Alice"
Write-Output $greeting  # Output: Hello, Alice!

In this example, the function Get-Greeting takes a single parameter $Name and returns a greeting string.

Returning a Single Value

You can return a value from a function either by using the return keyword or simply by outputting the value. Here’s an example:

function Get-Square {
    param (
        [int]$Number
    )
    return $Number * $Number
}

# Usage
$result = Get-Square -Number 5
Write-Output $result  # Output: 25

In this example, the function Get-Square calculates the square of the input number and returns it using the return keyword.

Alternatively, you can output the value directly without using return:

function Get-Square {
    param (
        [int]$Number
    )
    $Number * $Number
}

# Usage
$result = Get-Square -Number 5
Write-Output $result  # Output: 25

Both methods are valid, but using return can make it clearer that the function is intended to return a value.

Using Multiple Parameters

You can define functions with multiple parameters in a PowerShell function. Here’s an example:

function Add-Numbers {
    param (
        [int]$FirstNumber,
        [int]$SecondNumber
    )
    return $FirstNumber + $SecondNumber
}

# Usage
$sum = Add-Numbers -FirstNumber 10 -SecondNumber 20
Write-Output $sum 

In this example, the function Add-Numbers takes two parameters, adds them together, and returns the result.

PowerShell Function with Parameter and Returning Multiple Values

Here are a few examples of PowerShell function with parameter, that returning multiple values

Using an Array

Here is an example of a PowerShell function with parameter and return multiple values by using an array:

function Get-Coordinates {
    param (
        [int]$X,
        [int]$Y
    )
    return @($X, $Y)
}

# Usage
$coords = Get-Coordinates -X 10 -Y 20
Write-Output "X: $($coords[0]), Y: $($coords[1])"  # Output: X: 10, Y: 20

In this example, the function Get-Coordinates returns an array containing the X and Y coordinates.

Using a Hashtable

Here is an example of a PowerShell function with parameters that returns multiple values by using a Hashtable.

function Get-UserInfo {
    param (
        [string]$Name,
        [int]$Age
    )
    return @{
        Name = $Name
        Age  = $Age
    }
}

# Usage
$userInfo = Get-UserInfo -Name "Alice" -Age 30
Write-Output "Name: $($userInfo.Name), Age: $($userInfo.Age)"

Here, the function Get-UserInfo returns a hashtable with the user’s name and age.

You can see the output in the screenshot below:

powershell function with parameters and return value

Using a Custom Object

Here is an example of a PowerShell function with parameters that returns multiple values by using a custom object.

function Get-Employee {
    param (
        [string]$Name,
        [string]$Title
    )
    $employee = [PSCustomObject]@{
        Name  = $Name
        Title = $Title
    }
    return $employee
}

# Usage
$employee = Get-Employee -Name "John Doe" -Title "Manager"
Write-Output "Name: $($employee.Name), Title: $($employee.Title)"  # Output: Name: John Doe, Title: Manager

In this example, Get-Employee returns a custom object with properties Name and Title.

Conclusion

In this PowerShell tutorial, I have explained the PowerShell function with return values and shown some examples of PowerShell functions that take parameters and return single and multiple values.

You can return multiple values from a PowerShell function using an array, hashtable, or complex objects.

If you still have questions, feel free to leave a comment below.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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