Chapters/scripting-at-scale/pipelinesamples.ps1
#demonstrate pipeline performance differences return "This is a demo script file." #region pipeline vs parameter $names = Get-Service | Select-Object -ExpandProperty name #57ms Measure-Command { $names | Get-Service } #40ms Measure-Command { Get-Service $names } $big = $names+$names+$names+$names+$names #253ms Measure-Command { $big | Get-Service } #169ms Measure-Command { Get-Service $big } #endregion #region breakup a command #861ms Measure-Command { $data = Get-ChildItem $env:temp -file -Recurse | Group-Object -Property extension | Sort-Object -Property Count | Select-Object -Property Count,Name, @{Name="Size";Expression = {($_.group | Measure-object -Property length -sum).sum}} } #779ms Measure-Command { $files = Get-ChildItem $env:temp -file -Recurse $grouped = $files | Group-Object -Property extension $sorted = $grouped | Sort-Object -Property Count $data = $sorted | Select-Object -Property Count,Name, @{Name="Size";Expression = {($_.group | Measure-Object -Property length -sum).sum}} } #endregion #region ForEach vs Foreach-Object $n = 1..10000 #51.7ms Measure-command { $a = 0 foreach ($i in $n) { $a+=$i } } #227ms Measure-command { $n | foreach-object -Begin { $a = 0 } -process { $a+=$_ } } #endregion i |