Powershell: Why does (gci c:\ddd).count on an Empty folder not return 0 Powershell: Why does (gci c:\ddd).count on an Empty folder not return 0 powershell powershell

Powershell: Why does (gci c:\ddd).count on an Empty folder not return 0


Use the operator @() to make sure that the result is an array, including empty or containing a single item:

@(gci c:\ddd).count

Commands may return: 1) a collection; 2) a single object; 3) null. Your case is 3. Calling .Count on null (case 3) or on an object that does not have a property Count (case 2) gets nothing or may fail, for example, with strict mode enabled Set-StrictMode -Version 2.

@(...) is always an array and Count works.