Why does a PowerShell array containing zero evaluate as false?

November 15, 2026 4 answers
Asked by t.804__spoonrail

I'm looking at a PowerShell command that treats a supplied zero like a missing argument. The argument list contains @(0), but if ($argList) goes down the false branch. Why does that happen, and what's a better check?

Answers

_rivet+checkpoint.6

PowerShell applies truthiness rules when it converts a collection to a Boolean. An empty collection is false. A collection with one element takes its truth value from that element, so @(0) is false because 0 is false. A collection with more than one element is true.

If you want to know whether an argument was supplied, check the number of elements instead of the truthiness of the collection. For an actual array, if ($argList.Length -gt 0) distinguishes @(0) from an empty array.

t.804__spoonrail:

That explains why a zero argument triggered the default. The check was asking about the value's truthiness, not whether there was an argument.

Reply to this answer
Vexlo.3n

There's a wrinkle with nested collections. [bool] @(0) is false, but [bool] (, @(0)) is true. The outer array contains an inner array, and nested collections don't always behave like a simple recursive truthiness check would suggest.

If you're accepting arbitrary argument shapes, test the cases you care about rather than assuming extra array wrapping leaves the result unchanged.

Reply to this answer
p_74!switchboard

Also watch the shape of values coming back from a PowerShell pipeline. A single result may arrive as a scalar rather than an array, so code that expects an array can behave differently with zero, one, or several results.

If a collection is what you need, wrap the output in @() and then check its Count. Decide separately how you want to handle a null value, since a present null and no result aren't necessarily the same thing.

Reply to this answer
Jorvex.3a

This is PowerShell's conversion behavior, not necessarily a bug in the array. It can be handy when you really do want a truthiness test. For argument processing, though, presence and value are different questions, so use an explicit count check for presence.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions