Expand Array in PowerShell

In this PowerShell tutorial, I will explain how to expand an array in PowerShell using different methods and examples.

To expand an array in PowerShell, you typically use the += operator to add elements, which effectively creates a new array with the additional elements. For example, $array += ‘new item’ adds ‘new item’ to the end of $array. If you need to expand a property within an object array, you can use the Select-Object cmdlet with the -ExpandProperty parameter.

Expand Arrays in PowerShell

Expanding an array in PowerShell means to transform a collection of objects so you can work with their properties directly. This is commonly done with the Select-Object cmdlet using the -ExpandProperty parameter.

Here are a few ways to expand an array in PowerShell.

1. Using Select-Object with -ExpandProperty

The Select-Object cmdlet in PowerShell is used to select specific properties of an object or set of objects. When combined with the -ExpandProperty parameter, it allows you to take a property from each object in a collection and return a new array with just the values of that property.

Here’s an example using Get-Process to retrieve a list of processes and then expanding the ProcessName property:

$processNames = Get-Process | Select-Object -ExpandProperty ProcessName

This will return an array of strings, each one being the name of a process.

After executing the code using PowerShell, you can see the output in the screenshot below:

Expand Array in PowerShell

2. Using ForEach-Object

Another way to expand an array in PowerShell is by using the ForEach-Object cmdlet. This cmdlet allows you to perform an operation on each item within a collection. Here’s how you can use it to achieve similar results to the Select-Object -ExpandProperty:

$processNames = Get-Process | ForEach-Object { $_.ProcessName }

This script block { $_.ProcessName } is executed for each process object, and the process names are collected into the $processNames array.

3. Using Custom Functions

You can also create a custom function to expand an array in PowerShell. This method gives you more control over how the array is expanded and can be useful for more complex scenarios.

Here’s a simple custom function to expand an array of objects:

function Expand-ArrayProperty {
    param(
        [Parameter(Mandatory = $true)]
        [array]$Array,

        [Parameter(Mandatory = $true)]
        [string]$PropertyName
    )

    $expandedArray = @()
    foreach ($item in $Array) {
        $expandedArray += $item.$PropertyName
    }

    return $expandedArray
}

# Usage
$processes = Get-Process
$processNames = Expand-ArrayProperty -Array $processes -PropertyName "ProcessName"

This function, Expand-ArrayProperty, iterates over each item in the provided array and extracts the specified property, collecting the results into a new array.

Conclusion

Expanding arrays in PowerShell is a powerful technique that allows you to manipulate and access objects’ properties within an array efficiently. Whether you’re using built-in cmdlets like Select-Object with -ExpandProperty, iterating with ForEach-Object, or crafting your custom functions, understanding how to expand arrays will greatly enhance your PowerShell scripting capabilities.

In this PowerShell tutorial, I have explained how to expand an array in PowerShell using various methods like:

  1. Using Select-Object with -ExpandProperty
  2. Using ForEach-Object
  3. Using Custom Functions

You may also like: