10

The following code returns True. What's the correct way to check the type in PowerShell?

$e = @(1,2)
$e -ne $null -and $e.GetType() -eq (@("")).GetType()

Update:

The following code will return false. It's expected to be true.

$e = @('1','2')
$e -is [string[]]
3
  • 1
    Like this: $e -is "String[]" Commented Jan 6, 2020 at 17:35
  • For array, you can use $e -is [object[]]. Commented Jan 6, 2020 at 17:44
  • 1
    $e -is [array] will work as well (common ancestor) :) Commented Jan 6, 2020 at 17:44

1 Answer 1

12

You can use the -is operator to check the type to check if it's an array:

$arr = 1, 2
$arr -is [array]

However, it is probably more prudent to check that the object implements IEnumerable, since there are many collection types that are not specifically arrays but are iterable:

$arr = 1, 2
$arr -is [System.Collections.IEnumerable]

Also note that if you assign the result of a cmdlet/function to a variable that can return more than one element, but it only returns a collection with a single element, by default it will be unrolled and assigned as a single value. You can force a returned collection to be evaluated/assigned as an array with one of the following techniques (using Get-Process as an example of this):

$arr = @( Get-Process iexplore ) # ===========> Forces the returned value to be part of a collection,
                                 #              even if only a single process is returned.
$arr -is [System.Collections.IEnumerable] # ==> true

or you can use the , notation to force the returned values to be evaluated as a collection as well (you will need to sub-express your cmdlet/function call in this case):

$arr = , ( Get-Process iexplore )
$arr -is [System.Collections.IEnumerable]

If you are checking a generic collection (a collection whose type is defined within the System.Collections.Generic namespace), you can also check against the collection's associated type using System.Collections.Generic.IEnumerable[T] instead, where T is the type of element allowed in the array. For example:

$list = New-Object System.Collections.Generic.List[string]
$list -is [System.Collections.Generic.IEnumerable[string] # ==> true
Sign up to request clarification or add additional context in comments.

3 Comments

Is it a way to check if the type is IEnumerable<string>?
Yes, but make sure if you are using a generic collection of some sort that you check for System.Collections.Generic.IEnumerable, instead of System.Collections.IEnumerable, as the latter does not accept an element type to be defined. However, as System.Collections.Generic.IEnumerable[T] inherits from System.Collections.IEnumerable, you can still do an untyped IEnumerable check against a generic collection in this way.
Careful when testing your object is IEnumerable since non-arrays and non-generic lists may also enumerable, example: $Hashtable = @{}; $Hashtable -is [System.Collections.IEnumerable] also returns true. I realize this is outside the OP scope, and they likely have full control of their input object type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.