Skip to main content
infervour.com

Back to all posts

How to Return Several Items From A Powershell Function?

Published on
5 min read
How to Return Several Items From A Powershell Function? image

Best PowerShell Function Tools to Buy in October 2025

1 Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools

BUY & SAVE
$47.34 $59.99
Save 21%
Learn PowerShell Scripting in a Month of Lunches, Second Edition: Write and organize scripts and tools
2 Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects

BUY & SAVE
$0.99
Beginner’s Guide to PowerShell Scripting: Automate Windows Administration, Master Active Directory, and Unlock Cloud DevOps with Real-World Scripts and Projects
3 AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease

BUY & SAVE
$48.99
AWS Tools for PowerShell 6: Administrate, maintain, and automate your infrastructure with ease
4 Learn Windows PowerShell in a Month of Lunches

Learn Windows PowerShell in a Month of Lunches

BUY & SAVE
$34.99
Learn Windows PowerShell in a Month of Lunches
5 Learn PowerShell Toolmaking in a Month of Lunches

Learn PowerShell Toolmaking in a Month of Lunches

BUY & SAVE
$20.51 $44.99
Save 54%
Learn PowerShell Toolmaking in a Month of Lunches
6 PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)

BUY & SAVE
$37.95
PowerShell Advanced Cookbook: Enhance your scripting skills and master PowerShell with 90+ advanced recipes (English Edition)
7 PowerShell for Sysadmins: Workflow Automation Made Easy

PowerShell for Sysadmins: Workflow Automation Made Easy

BUY & SAVE
$28.99
PowerShell for Sysadmins: Workflow Automation Made Easy
8 Hands-On Penetration Testing on Windows: Unleash Kali Linux, PowerShell, and Windows debugging tools for security testing and analysis

Hands-On Penetration Testing on Windows: Unleash Kali Linux, PowerShell, and Windows debugging tools for security testing and analysis

BUY & SAVE
$22.26 $48.99
Save 55%
Hands-On Penetration Testing on Windows: Unleash Kali Linux, PowerShell, and Windows debugging tools for security testing and analysis
+
ONE MORE?

Returning several items from a PowerShell function can be accomplished using different approaches. Here are three commonly used methods:

  1. Array: You can create an array in your function and add multiple items to it. By returning the array as the output of the function, you can access all the items. Here's an example:

function Get-MultipleItems { $items = @("Item1", "Item2", "Item3") return $items }

  1. Hashtable: A hashtable allows you to associate multiple values with unique keys. By returning a hashtable, you can access different items using their respective keys. Here's an example:

function Get-MultipleItems { $items = @{ "Key1" = "Item1" "Key2" = "Item2" "Key3" = "Item3" } return $items }

  1. Custom Object: You can create a custom PowerShell object with properties representing the different items. By returning this object, you can access each item as a property value. Here's an example:

function Get-MultipleItems { $item1 = "Item1" $item2 = "Item2" $item3 = "Item3"

$itemObject = \[PSCustomObject\]@{
    Item1 = $item1
    Item2 = $item2
    Item3 = $item3
}

return $itemObject

}

These methods allow you to return multiple items from a PowerShell function while providing different ways to access and utilize the returned values.

How to return multiple boolean values from a PowerShell function?

In PowerShell, you cannot directly return multiple boolean values from a function. However, you can achieve the same result by using other data types such as arrays or PowerShell objects. Here are two approaches you can take:

  1. Using an array: You can create an array to store multiple boolean values and return that array from the function. Here's an example:

function Get-Booleans { $result = @() $result += $true $result += $false return $result }

$booleans = Get-Booleans $boolean1 = $booleans[0] $boolean2 = $booleans[1]

  1. Using a PowerShell object: You can create a custom PowerShell object with properties representing the boolean values and return that object from the function. Here's an example:

function Get-Booleans { $result = New-Object -TypeName PSObject $result | Add-Member -MemberType NoteProperty -Name "Boolean1" -Value $true $result | Add-Member -MemberType NoteProperty -Name "Boolean2" -Value $false return $result }

$booleans = Get-Booleans $boolean1 = $booleans.Boolean1 $boolean2 = $booleans.Boolean2

Both approaches allow you to return and access multiple boolean values from a PowerShell function. Choose the one that suits your needs best.

How to return an object with multiple properties from a PowerShell function?

To return an object with multiple properties from a PowerShell function, you can use the New-Object cmdlet to create a custom object and define its properties. Here's an example:

function Get-CustomObject { $property1 = "Value 1" $property2 = "Value 2" $property3 = "Value 3"

$customObject = New-Object -TypeName PSObject -Property @{
    Property1 = $property1
    Property2 = $property2
    Property3 = $property3
}

return $customObject

}

Call the function and store the returned object in a variable

$result = Get-CustomObject

Access the properties of the returned object

Write-Host $result.Property1 Write-Host $result.Property2 Write-Host $result.Property3

In this example, the Get-CustomObject function creates a custom object with three properties: Property1, Property2, and Property3. The function uses the New-Object cmdlet to define the properties and their corresponding values. Finally, the function returns the custom object, which can be assigned to a variable and accessed later.

How to return a collection of items from a PowerShell function?

To return a collection of items from a PowerShell function, you can use the return keyword followed by the collection of items you want to return. Here's an example of a function that returns an array of strings:

function Get-Fruit { $fruits = @("Apple", "Banana", "Orange") return $fruits }

$fruitCollection = Get-Fruit

In this example, the Get-Fruit function creates an array of strings called $fruits and returns it using the return keyword. The returned collection is then assigned to the variable $fruitCollection in the calling code.

You can also use the output pipeline to implicitly return a collection of items from a function. Here's an example:

function Get-Fruit { $fruits = @("Apple", "Banana", "Orange") Write-Output $fruits }

$fruitCollection = Get-Fruit

In this case, the Write-Output cmdlet is used to send the collection of fruits to the output pipeline, which will implicitly return them from the function. The assignment to $fruitCollection is done in the same way as before.

Both examples will result in the variable $fruitCollection containing the collection of fruits returned from the function.

What is the default behavior when returning multiple items in PowerShell?

When a function or command returns multiple items in PowerShell, the default behavior is to output each item as a separate member of an array. This means that multiple objects will be returned as an array of objects, each item occupying a separate index in the array.

For example, if a command returns two strings "Hello" and "World", the default behavior will be to return an array containing the two strings: "Hello", "World".

However, if the returned items are being assigned to a variable, PowerShell will automatically unroll the array and assign each item to a separate variable. This is called "unrolling" or "unrolling the pipeline". In this case, each item will be assigned to a separate variable in the order they appear in the array.

For example, if a command returns two strings "Hello" and "World" and the result is assigned to a variable $result, PowerShell will unroll the array and assign "Hello" to $result[0] and "World" to $result[1].

It is also possible to explicitly control the default behavior by using array operators like comma (,) to force the returned items to be treated as separate items or by using the -OutputAs parameter in certain cmdlets to specify a specific output format.