运行 直接在 CMD 上的 PowerShell 命令给出索引超出范围错误

Running PowerShell Command Directly On CMD Gives Index Was Out Of Range Error

下面的命令用于列出过期的用户帐户:

powershell -c "Get-LocalUser | Where-Object { $_.AccountExpires -le (Get-Date) -and $null -ne $_.AccountExpires } | Select-Object Name, AccountExpires"

运行 直接在 PowerShell 上似乎 运行 很好,但是,当我通过 CMD 运行 相同的命令时,它给出以下错误:

Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index').

我认为问题与 $null 参数有关,但我需要它来从输出中排除空白匹配项。有谁知道修复此错误的方法?或者不匹配空白输出的替代方法?

使用运算符时要明确:

powershell -c "Get-LocalUser | Where-Object { ($_.AccountExpires -le (Get-Date)) -and ($null -ne $_.AccountExpires) } | Select-Object Name, AccountExpires"

这对我来说很好

可能不是 $null,请尝试使用空字符串:

("" -ne $_.AccountExpires)