Testing For an Array in Powershell

I’ve seen lots of methods for checking whether or not a variable in Powershell contains an array.  All of them work, so really it comes down to which one is the easiest.  Some cmdlets and constructs will error out if you treat them like an array, or more commonly, produce unexpected results. Here are the two methods I’ve used for this over the years, I’ll let you be the judge of which one is the easiest.

Method 1: Using the ‘-is’ operator

Method 2: Using the Count property

Hope this helps make your code a little cleaner and remember; the next data you validate, could be yours. Happy scripting!

Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *