为什么 "where" 没有像我期望的那样工作 select 某些结果?

Why doesn't "where" work as I would expect here to select certain results?

我是 运行 下面的 PowerShell,我的问题是为什么第二个命令 return 没有任何结果?另外,我应该使用 where 还是 Where-Object?请参阅下面的屏幕截图。

Write-Host "This displays all..." -ForegroundColor Green
Get-Command -Module "Microsoft.TeamFoundation.PowerShell"

Write-Host "This displays nothing..." -ForegroundColor Yellow
Get-Command -Module "Microsoft.TeamFoundation.PowerShell" | Where-Object ($_.Name -like '*Tfs*')

Where-Object 需要脚本块或比较语句 (3.0+)。

在您的情况下,用大括号替换圆括号会起作用:

Get-Command -Module "Microsoft.TeamFoundation.PowerShell" |
    Where-Object {$_.Name -like '*Tfs*'}

或者您可以使用比较语句:

Get-Command -Module "Microsoft.TeamFoundation.PowerShell" |
    Where-Object -Property Name -like '*Tfs*'