Powershell Test if array in one line Powershell Test if array in one line powershell powershell

Powershell Test if array in one line


There are type operators one can use to test the type of a variable. -is is the one you need. Like so,

$foo = @()        # Array$bar = "zof"      # String$foo -is [array]  # Is foo an array?True              # Yes it is$foo -is [string] # Is foo a string?False             # No it is not$bar -is [array]  # How about barFalse             # Nope, not an array$bar -is [string] # A string then?True              # You betcha!

So something like this could beused

if($csv -is [array]) {    # Stuff for array} else {    # Stuff for string}


Instead of doing:

$csv = (gc $FileIn)

you had to

$csv = @(gc $FileIn)

Now the output will always be an array of strings irrespective of the file having one line or not. The rest of the code will just have to treat $csv as an array of strings. This way is better than having to check if the output is an array etc., at the least in this situation.