Arrays in PowerShell are quite forgiving. They are easy to declare, and they can contain elements of different types if you want to. You can quite easily check to see if an object is an array or not, but have you ever needed to check the types of the elements inside the array itself?
Let’s start by declaring a few test arrays that we can work with:
Here I have created five different arrays, in different ways. You probably know that you can check the type of objects in PowerShell by using the GetType method. Let’s try that with the first array:
As you can see, the base type of the object is of type ‘System.Array’. Ok, let’s try to do the same with the fourth array:
Do you see the difference? The base type is the same, but the type name is given as ‘Object[]’ in the first example, but ‘String[]’ in the second example. The reason for this is how we declared the arrays. $a1 was not declared as any specific array at all, PowerShell just guessed it by the input we gave it. On the other hand, $a4 was declared as an array of strings.
Here are the result of using GetType on all our arrays, and you can clearly see the difference between the two ways of declaring an array.
What do you do if you need to know the types of the elements of an array as opposed to the type of the object (array) itself then?
As we see in the above examples, it’s easy if the array was declared as an array of some type. Then you can just use the GetType method and check the Name property of the TypeInfo object returned. But for the first example, or for any arrays containing elements of different types, it’s not so easy.
To help us in this task, I created a small helper-function I called Get-TypeName. Let’s check it out:
This function checks the elements inside an object and returns the different types it finds. That’s why it returns 7 types, even though we only declared 5 arrays. Remember, one of them contained a mix of different types.
So, this is all nice and all, but do it have any practical application? Ok, let’s assume you have a script where you have to work with an array, but you need to know that all elements in the array are of the same type! Perhaps you are iterating on its members and run further commands that only supports a specific type?
Using the Get-TypeName function you can easily check to see if all elements are of the same type by doing the following:
if (-not((Get-TypeName -InputObject $array) -is [array])) { 'All elements in the array are of the same type' } else { 'Array contains a mix of different types' }
Here I’m leveraging the fact that the output of the function will only be an array if the input object contains more than one type, and you can easily branch your logic to handle both cases.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-TypeName { | |
param ($InputObject) | |
Write-Output (($InputObject | Get-Member).TypeName | Select-Object –Unique) | |
} |
If you found this helpful, spotted any errors, or have an easier way of achieving the same result, I would love to get your feedback in the comments section below!