How to Convert Object to Array in PowerShell?

Do you need to convert an object to an array in PowerShell? In this PowerShell tutorial, I will explain how to convert the object to an array in Powershell using various methods.

To convert an object to an array in PowerShell, you can cast the object to an array type by prefixing it with [array], such as $array = [array]$object. Alternatively, you can use the array subexpression operator @(), like $array = @($object), to ensure the result is an array even if the object contains only a single value.

Convert Object to Array in PowerShell

Before diving into the conversion process, let’s understand what objects and arrays are in the context of PowerShell.

An object in PowerShell is an instance of a .NET class that represents structured data. It can contain multiple properties and methods, which can be accessed using the dot notation (e.g., $object.PropertyName).

An array, on the other hand, is a data structure designed to store a collection of items. These items can be of the same type or different types. Arrays are useful when you need to iterate over a set of values or access them by their index.

Now, let us check different methods to convert an object to an array in PowerShell.

1. Using the @() Array Subexpression Operator

One of the simplest ways to convert an object to an array is by using the array subexpression operator @() in PowerShell. This operator ensures that the result is always an array, even if the input is a single object.

Here is a complete example.

$object = New-Object PSObject -Property @{
    Name = 'John'
    Age = 30
}

$array = @($object)

In this example, $object is a single PowerShell custom object with properties Name and Age. By wrapping $object with @(), it is converted into an array with one element.

You can see the output in the screenshot below after I executed the script using VS code.

Convert Object to Array in PowerShell

2. Casting with [array]

Another method to convert an object to an array is by explicitly casting the object as an array type using [array] in PowerShell.

Here is a complete example.

$object = New-Object PSObject -Property @{
    Name = 'Jane'
    Age = 25
}

$array = [array]$object

Here, [array]$object casts the $object to an array type. If $object was already an array, this operation would have no effect, but if it’s not, $object will be wrapped in an array.

3. Using the += Operator to Build an Array

You can also use the += operator to add an object to an existing array or to create a new array by adding the object to $null in PowerShell.

Here is a complete example.

$object = New-Object PSObject -Property @{
    Name = 'Alice'
    Age = 28
}

$array = $null
$array += $object

In this example, we start with $array as $null and then add $object to it. The result is that $array becomes an array with $object as its first element.

4. Using the ForEach-Object Cmdlet

If you have a collection of objects and want to convert them into an array, you can use the ForEach-Object cmdlet in PowerShell. This is particularly useful when dealing with the output of a cmdlet that returns multiple objects.

Here is a complete PowerShell script.

$objects = Get-Process | Select-Object -First 5
$array = $objects | ForEach-Object { $_ }

In this example, Get-Process retrieves the current processes running on the system, and Select-Object -First 5 limits the output to the first five processes. The ForEach-Object cmdlet iterates over each object in $objects and outputs them into $array.

5. Using the .ToArray() Method with ArrayList

For collections that support it, such as an ArrayList, you can use the .ToArray() method to convert the collection into a standard array.

Here is an example of how to convert an object to an array using the .ToArray() method in PowerShell.

$arrayList = New-Object System.Collections.ArrayList
$arrayList.Add((New-Object PSObject -Property @{Name = 'Bob'; Age = 27}))
$array = $arrayList.ToArray()

In this example, an ArrayList is created and a new object is added to it. The .ToArray() method is then called on the ArrayList to convert it into a standard PowerShell array.

Conclusion

PowerShell provides several ways to convert objects to arrays in PowerShell. In this PowerShell tutorial, I have explained various methods to convert an object to an array in PowerShell like:

  1. Using the @() Array Subexpression Operator
  2. Casting with [array]
  3. Using the += Operator to Build an Array
  4. Using the ForEach-Object Cmdlet
  5. Using the .ToArray() Method with ArrayList

You may also like: