Powershell:为什么Empty文件夹上的(gci c:\ ddd).count不返回0

icn*_*vad 4 powershell null count get-childitem

为什么(gci c:\ddd).count在Empty文件夹中不返回0但"没有"

我只是得到一个错误"你不能在空值表达式上调用方法." 当我的计数条件不匹配时.

我需要"获取"零以防止异常?

Rom*_*min 7

使用运算符@()确保结果是数组,包括空或包含单个项:

@(gci c:\ddd).count
Run Code Online (Sandbox Code Playgroud)

命令可能返回:1)集合; 2)单个对象; 3)null.你的情况是3.调用.Countnull(情况3)或没有属性的对象Count(情况2)什么也没有或者可能失败,例如,启用了严格模式Set-StrictMode -Version 2.

@(...)永远是一个阵列和Count工作.