How to test for $null array in PowerShell How to test for $null array in PowerShell powershell powershell

How to test for $null array in PowerShell


It's an array, so you're looking for Count to test for contents.

I'd recommend

$foo.count -gt 0

The "why" of this is related to how PSH handles comparison of collection objects


You can reorder the operands:

$null -eq $foo

Note that -eq in PowerShell is not an equivalence relation.


if($foo -eq $null) { "yes" } else { "no" }help about_comparison_operators 

displays help and includes this text:

All comparison operators except the containment operators (-contains, -notcontains) and type operators (-is, -isnot) return a Boolean value when the input to the operator (the value on the left side of the operator) is a single value (a scalar). When the input is a collection of values, the containment operators and the type operators return any matching values. If there are no matches in a collection, these operators do not return anything. The containment operators and type operators always return a Boolean value.