2016-04-03 32 views
1

我在PSCustomObject中探索,PowerShell ISE自动完成向我展示了一些有趣的东西,我似乎无法找到更多信息。PSCustomObject和过载方法其中

如果你有一个包含PSCustomObject的变量,似乎有一个不容易找到的过滤方法。

这种过滤方法比使用管道和Where-Object更快吗?

$CusomObject.Where({$_ -like '*hidden*'}) 

使用$CustomObject | Get-Member -Force不会列出此过载。我发现reference to this overload here,但似乎无法在我的搜索中找到很多用途。

enter image description here

回答

0

这只是似乎执行一个脚本块,并确认收益为True。从有限的测试中,PS对象的哪里更快(〜1秒)。注意:Stopwatch可能不准确。 Where-Object具有文档并且执行速度比ForEach-Object快。 BeWhere PSv4中存在错误。

如果脚本块

write-host "Started at $(get-date)" 
$j = $i.Where({$_ -like "*"}) 
write-host "Ended at $(get-date)" 
write-host "Total Elapsed Time: $($elapsed.Elapsed.ToString())" 

00:00:00.0309266

ForEach-Object

$elapsed = [System.Diagnostics.Stopwatch]::StartNew() 
write-host "Started at $(get-date)" 
$j = $i | %{$_ -like "*"} 
write-host "Ended at $(get-date)" 
write-host "Total Elapsed Time: $($elapsed.Elapsed.ToString())" 

00:00:01.0555753

Where-Object

$elapsed = [System.Diagnostics.Stopwatch]::StartNew() 
write-host "Started at $(get-date)" 
$j = $i | ?{$_ -like "*"} 
write-host "Ended at $(get-date)" 
write-host "Total Elapsed Time: $($elapsed.Elapsed.ToString())" 

00:00:00.0348174