How do I get (gci).count to return proper numeric values for empty or single-item lists?

Solution 1:

This has to do with the fact that powershell, when returning an array of 1 item will simply collapse the array into that single item.

Before version 3, this would result is an object without the expected count property and your result would be empty. This has been "fixed" in version 3.

So you have 2 options:

  1. Upgrade to Powershell v3
  2. Apply the following workaround where you always wrap your results as an array type @():

    @(gci).count