Home » How to Get Length of an Array in PowerShell

How to Get Length of an Array in PowerShell

To get the length of an array in PowerShell, you can use the Length property or its Count alias. It returns the number of items in an array.

Method 1: Get the Length of an array using the Length property

$languages = @('Python','PowerShell','C#','Java','Go')
$languages.Length

This example uses the Length property to find the number of elements in an array.

Method 2: Count objects in an array using the Count property

$languages = @('Python','PowerShell','C#','Java','Go')
$languages.Count

This example uses the Count property to get the total count of objects in an array.

Get the Length of an Array Using the Length Property

To get the number of elements in an array in PowerShell, use the Length property.

The following example shows how to create an array using @() and return the length of an array.

$languages = @('Python','PowerShell','C#','Java','Go')
$languages.Length

In this PowerShell script, the $languages array contains the names of programming languages. The Length property of the array variable $languages returns the number of items in an array.

The output of the above PowerShell script is given below.

PS C:\> $languages = @('Python','PowerShell','C#','Java','Go')

PS C:\> $languages.Length

5

Count Objects in an Array Using the Count Property

To get the number of elements in an array in PowerShell, use the Count property.

The following example shows how to create an array using @() and return the count objects in an array.

$languages = @('Python','PowerShell','C#','Java','Go')
$languages.Count

In this PowerShell script, the $languages array contains the names of programming languages. The Count property of the array variable $languages returns the number of elements in an array.

The output of the above PowerShell script is given below.

PS C:\> $languages = @('Python','PowerShell','C#','Java','Go')

PS C:\> $languages.Count

5

Conclusion

I hope the above article on how to get the length of an array in PowerShell using the Length and Count property is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.