How to Get Array Length in PowerShell?

Do you need to get the PowerShell array length? In this PowerShell tutorial, I will explain how to get array length in PowerShell using various methods.

To determine the length of an array in PowerShell, use the Length property on the array variable. For instance, if $array is your array, $array.Length will return the number of elements in it. This property provides a simple and effective way to know the size of any array in PowerShell.

PowerShell Get Array Length

Now, let us see different methods to get array length in PowerShell.

Using the Count Property

The most straightforward method to get the length of an array in PowerShell is to use the Count property. This property returns the total number of elements in the array.

Here’s how you can use it:

# Define an array
$myArray = @(1, 2, 3, 4, 5)

# Get the array length using the Count property
$arrayLength = $myArray.Count

# Display the length
Write-Host "The array length is: $arrayLength"

In this example, $myArray is an array containing five elements. Using $myArray.Count, we store the length of the array in the $arrayLength variable and then print it to the console.

You can see the screenshot below after I ran the code using Windows PowerShell ISE.

PowerShell Get Array Length

Using the Length Property

Another way to determine the size of an array is by using the Length property in PowerShell. This property serves the same purpose as Count.

Here’s an example:

# Define an array
$myArray = @(1, 2, 3, 4, 5)

# Get the array length using the Length property
$arrayLength = $myArray.Length

# Display the length
Write-Host "The array length is: $arrayLength"

This will produce the same result as the previous example. The Length property is a versatile option as it can be used with arrays and other collection types in PowerShell.

Using the Measure-Object Cmdlet

For a more PowerShell-centric approach, you can use the Measure-Object cmdlet. This cmdlet is generally used to measure various properties of objects, including the count of array elements.

Here’s how you can use Measure-Object to get the array length:

# Define an array
$myArray = @(1, 2, 3, 4, 5)

# Use Measure-Object to get the count
$arrayLength = $myArray | Measure-Object | Select-Object -ExpandProperty Count

# Display the length
Write-Host "The array length is: $arrayLength"

In this example, $myArray is piped into Measure-Object, which calculates the count. The Select-Object -ExpandProperty Count is used to extract just the count value.

You can see the output after I ran the code using Visual Studio Code.

How to Get Array Length in PowerShell

Conclusion

Determining the length of an array in PowerShell is a simple process that can be accomplished using the Count or Length properties or by utilizing the Measure-Object cmdlet.

For beginners, starting with the Count or Length properties is often the easiest approach. As you become more comfortable with PowerShell, incorporating cmdlets like Measure-Object can offer more flexibility and align with PowerShell’s object-oriented nature.

Remember that arrays in PowerShell are zero-indexed, meaning the first element is at index 0, and the last element is at index Count - 1 or Length - 1. In this PowerShell tutorial, I have explained how to get the length of an array in PowerShell using different methods.

You may also like: