How to Check if a Variable is an Array in PowerShell?

While working with PowerShell, you might need to check if a variable is an array in PowerShell. In this PowerShell tutorial, I will explain how to check if a variable is an array in PowerShell.

To check if a variable is an array in PowerShell, use the -is operator followed by [array]. For instance, the expression $variable -is [array] will yield True if $variable is indeed an array, or False if it is not. This is a quick and effective way to validate the type of a variable.

How to Check if a Variable is an Array in PowerShell

Now, let us check if a variable is an array in PowerShell using various methods.

Using the -is Operator

The most straightforward method to check if a variable is an array in PowerShell is by using the -is operator. This operator allows you to test if a variable is a certain data type. Here’s a simple example:

$a = 1,2,3,4,5
$a -is [array]

If $a is an array, the output of the second line will be True. Conversely, if $a is not an array, it will return False.

You can check the output in the screenshot below:

Check if a Variable is an Array in PowerShell

Utilizing the .GetType() Method

Another way to determine if a variable is an array is by using the .GetType() method and checking its Name property. Here’s how you can do it:

$a = 1,2,3,4,5
$type = $a.GetType()
$type.Name -eq 'Object[]'

If $a is an array, the comparison -eq 'Object[]' will return True.

Checking the Count Property

Arrays in PowerShell have a Count property that returns the number of elements in the array. You can use this property to check if a variable is an array or not. However, this method is not foolproof because single-item variables can be coerced into arrays and may also have a Count property. Here’s an example:

$a = 1,2,3,4,5
$a.Count

The $a.Count will return the number of elements in the array $a. If $a is not an array, PowerShell will attempt to convert $a to an array and then return the count. This means that even single-item variables will return a count of 1.

Using Custom Functions

You can write a custom function to check for an array for a more robust solution. This function can use a combination of the abovementioned methods to ensure accurate detection. Here’s a sample function:

function Test-IsArray($variable) {
    return $variable -is [array] -or ($variable.GetType().Name -eq 'Object[]')
}

$a = 1,2,3,4,5
Test-IsArray $a

This function, Test-IsArray, will return True if $a is an array and False otherwise.

Conclusion

If you want to identify whether a variable is an array in PowerShell, then you can use various methods. By using the -is operator, the .GetType() method, checking the Count property, or writing custom functions, you can accurately determine the data type of your variables.

Remember that while the Count property can be a quick way to check for an array, it’s not always reliable for single-item variables. The -is operator and .GetType() method are more accurate for identifying arrays. Custom functions offer flexibility and can combine multiple checks for a more thorough verification.

In this PowerShell tutorial, I have explained how to Check if a Variable is an Array in PowerShell using various methods.

You may also like: