{"id":17931,"date":"2010-06-27T00:01:00","date_gmt":"2010-06-27T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/06\/27\/hey-scripting-guy-weekend-scripter-timing-the-windows-powershell-pipeline\/"},"modified":"2010-06-27T00:01:00","modified_gmt":"2010-06-27T00:01:00","slug":"hey-scripting-guy-weekend-scripter-timing-the-windows-powershell-pipeline","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-timing-the-windows-powershell-pipeline\/","title":{"rendered":"Hey, Scripting Guy! Weekend Scripter: Timing the Windows PowerShell Pipeline"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><!-- AddThis Button BEGIN --><\/p>\n<p>\n<!--\nvar addthis_config = {\"data_track_clickback\":true};\n\/\/ -->\n<a href=\"http:\/\/www.addthis.com\/bookmark.php?v=250&amp;username=scriptingguys\" class=\"addthis_button\"><img decoding=\"async\" height=\"16\" width=\"125\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" alt=\"Bookmark and Share\" style=\"border:0\" \/><\/a><\/p>\n<p><!-- AddThis Button END --><\/p>\n<p>&nbsp;<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. I decided to get up early this morning and head out with my laptop, a camera, and my panama hat. My destination? <a href=\"http:\/\/en.wikipedia.org\/wiki\/Battery_Park_(Charleston)\">Battery Park<\/a>. The Scripting Wife and I are in <a href=\"http:\/\/en.wikipedia.org\/wiki\/Charleston,_SC\">Charleston<\/a>, <a href=\"http:\/\/en.wikipedia.org\/wiki\/South_carolina\">South Carolina<\/a>, for the weekend, and I thought it would be a great time to write some scripts. <\/p>\n<p>The <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/scripting+wife\/\">Scripting Wife<\/a> has a tendency <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/04\/18\/hey-scripting-guy-april-18-2010.aspx\">to hibernate on Saturday mornings<\/a>, and I learned a long time ago to be quiet. A hibernating bear is more safely awakened than a hibernating Scripting Wife. I therefore quietly escaped into the already hot and humid Charleston summer morning, and I went on a scouting mission to find a decent cup of tea. After checking out several different places&mdash;each of whom thought that a tea bag filled with what appeared to be <a href=\"http:\/\/en.wikipedia.org\/wiki\/Crabgrass\">crab grass<\/a> was a fine morning brew&mdash;I gave up and went to a little pastry shop that sold <a href=\"http:\/\/en.wikipedia.org\/wiki\/Beignet\">beignets<\/a> and a cup of coffee to go. As it turned out, the beignets were actually good, but the coffee tasted like burned cardboard. I am all for the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2008\/10\/02\/how-can-i-reuse-functions.aspx\">waste hierarchy<\/a>; I just do not think it has any place in morning beverages. I finally made it to Battery Park and was accosted by the little furry guy you see just below. I think he wanted one of my beignets, but I was able to snap his picture and still safeguard my breakfast. <\/p>\n<p><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/june\/hey0627\/wes-06-27-10-01.jpg\"><img decoding=\"async\" height=\"379\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6507.wes062710011_7CD2CACA.jpg\" alt=\"Photo of Beignet the Scripting Squirrel\" border=\"0\" title=\"Photo of Beignet the Scripting Squirrel\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/> <\/a><\/p>\n<p>After getting rid of all the powdered sugar from my hands, I opened up my laptop and went to work. I had an idea for improving the performance of my ParseScriptCommands.ps1 script from <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/06\/26\/hey-scripting-guy-weekend-scripter-playing-around-with-the-windows-powershell-tokenizer.aspx\">yesterday<\/a>, and I wanted to test it out. <\/p>\n<p>I have often heard that <a href=\"http:\/\/technet.microsoft.com\/en-us\/scriptcenter\/powershell.aspx\">Windows PowerShell<\/a> is inefficient when it comes to using the pipeline, and that it is actually quicker to store stuff in a variable and then iterate through the variable by using the <strong>Foreach<\/strong> statement. To be honest, I have not seen this behavior, but I have heard about it and have read about it on several blogs. It actually seems counterintuitive, but hey, who knows. <\/p>\n<p>When my ParseScriptCommands.ps1 script took several minutes to run (I have more than 1,300 scripts in my c:\\data\\psextras folder), I decided to put this claim to a test. The modified ModifiedParseScriptCommands.ps1 script is shown here.<\/p>\n<p><strong>ModifiedParseScriptCommands.ps1<\/strong><\/p>\n<p><span style=\"color: #2b91af\">$errors<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$null<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$logpath<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;C:\\a\\commandlog.txt&#8221;&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;C:\\data\\PSExtras&#8221;&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$scripts<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Get-ChildItem<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Include<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">*.ps1<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Recurse<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">ForEach<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$s<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$scripts<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$script<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$s<\/span><span style=\"color: #000000\">.fullname<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$scriptText<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">get-content<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$script<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[system.management.automation.psparser]::Tokenize(<\/span><span style=\"color: #2b91af\">$scriptText<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">[ref]<\/span><span style=\"color: #2b91af\">$errors<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">Foreach-object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Begin<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/>&#8220;Processing $script&#8221; <\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Out-File<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-FilePath<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logPath<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Append<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">`<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">-process<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$_<\/span><span style=\"color: #000000\">.<\/span><span style=\"color: #0000ff\">type<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #808080\"> &#8220;command&#8221;<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\"> &#8220;`t $($_.content)&#8221; <\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Out-File<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-FilePath<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logpath<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Append<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">notepad<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logpath<\/span> <\/p>\n<p>Only two lines were changed in the script. The first one stores the results of the <a href=\"http:\/\/www.bing.com\/visualsearch?g=powershell_cmdlets&amp;FORM=Z9GE22#toc=0&amp;s=2&amp;r=2\">Get-ChildItem cmdlet<\/a> in the <strong>$Scripts<\/strong> variable, which is shown here:<\/p>\n<p><span style=\"color: #2b91af\">$scripts<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Get-ChildItem<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Include<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">*.ps1<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Recurse<\/span> <\/p>\n<p>The second change moves from the <a href=\"http:\/\/www.bing.com\/visualsearch?g=powershell_cmdlets&amp;FORM=Z9GE22#toc=0&amp;s=2&amp;r=7\">ForEach-Object cmdlet<\/a> to the <strong>ForEach<\/strong> statement. This change is shown here: <\/p>\n<p><span style=\"color: #0000ff\">ForEach<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$s<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$scripts<\/span><span style=\"color: #000000\">)<\/span> <\/p>\n<p>I commented out the line in each script that opens the log in Notepad, and I used my Test-TwoScripts.ps1 script to evaluate the changes. The Test-TwoScripts.ps1 script is shown here.<\/p>\n<p><strong>Test-TwoScripts.ps1<\/strong><\/p>\n<p><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$baseLineScript<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$modifiedScript<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[int]<\/span><span style=\"color: #2b91af\">$numberOfTests<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[switch]<\/span><span style=\"color: #2b91af\">$log<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">param<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-Scripts<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$baseLineScript<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[string]<\/span><span style=\"color: #2b91af\">$modifiedScript<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">param<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">Measure-Command<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Expression<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Invoke-Expression<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-command<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$baseLineScript<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">Measure-Command<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Expression<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Invoke-Expression<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-command<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$modifiedScript<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-Scripts<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-Change(<\/span><span style=\"color: #2b91af\">$baseLine<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$modified<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">((<\/span><span style=\"color: #2b91af\">$baseLine<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&#8211;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$modified<\/span><span style=\"color: #000000\">)\/<\/span><span style=\"color: #2b91af\">$baseLine<\/span><span style=\"color: #000000\">)*<\/span><span style=\"color: #800000\">100<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-Change<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">Function<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-TempFile<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">[io.path]::GetTempFileName()<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-TempFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">function<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">***<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Entry<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Point<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">To<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Script<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$log<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Get-TempFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$results<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$modified<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$baseLine<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$null<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">For(<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #808080\">&amp;nbsp\n;<\/span><span style=\"color: #0000ff\">-le<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$numberOfTests<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$i++<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/>&#8220;Test $i of $numberOfTests&#8221; <\/span><span style=\"color: #0000ff\">;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">start-sleep<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-m<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">50<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$results<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Test-Scripts<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-baseLineScript<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$baseLineScript<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-modifiedScript<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$modifiedScript<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$baseLine<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">+<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$results<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #000000\">].TotalSeconds<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$modified<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">+<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$results<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">].TotalSeconds<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">If<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$log<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/>&#8220;$baseLineScript run $i of $numberOfTests $(get-date)&#8221; <\/span><span style=\"color: #000000\">&gt;&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logFile<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$results<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #800000\">0<\/span><span style=\"color: #000000\">]<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logFile<\/span><span style=\"color: #808080\">&nbsp; <br \/>&#8220;$modifiedScript run $i of $numberOfTests $(get-date)&#8221; <\/span><span style=\"color: #000000\">&gt;&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logFile<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #2b91af\">$results<\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">]<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logFile<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$log<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">for<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #808080\">&nbsp; <br \/>&#8220;Average change over $numberOfTests tests&#8221;&nbsp; <br \/>&#8220;BaseLine: $baseLineScript average Total Seconds: $($baseLine\/$numberOfTests)&#8221;&nbsp; <br \/>&#8220;Modified: $modifiedScript average Total Seconds: $($modified\/$numberOfTests)&#8221;&nbsp; <br \/>&#8220;Percent Change: &#8221; <\/span><span style=\"color: #000000\">+<\/span><span style=\"color: #808080\"> &#8220;{0:N2}&#8221; <\/span><span style=\"color: #000000\">-f<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(Get-Change<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-baseLine<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$baseLine<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-modified<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$modified<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$log<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp; <br \/>&#8220;Average change over $numberOfTests tests&#8221; <\/span><span style=\"color: #000000\">&gt;&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logFile<\/span><span style=\"color: #808080\">&nbsp; <br \/>&#8220;BaseLine: $baseLineScript average Total Seconds: $($baseLine\/$numberOfTests)&#8221; <\/span><span style=\"color: #000000\">&gt;&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logFile<\/span><span style=\"color: #808080\">&nbsp; <br \/>&#8220;Modified: $modifiedScript average Total Seconds: $($modified\/$numberOfTests)&#8221; <\/span><span style=\"color: #000000\">&gt;&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logFile<\/span><span style=\"color: #808080\">&nbsp; <br \/>&#8220;Percent Change: &#8221; <\/span><span style=\"color: #000000\">+<\/span><span style=\"color: #808080\"> &#8220;{0:N2}&#8221; <\/span><span style=\"color: #000000\">-f<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">(Get-Change<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-baseLine<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$baseLine<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-modified<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$modified<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">&gt;&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logFile<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#if<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$log<\/span><span style=\"color: #808080\">&nbsp; <br \/><\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$log<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">Notepad<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$logFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">}<\/span> <\/p>\n<p>The results of the change are seen in the following image. The modified script actually took nine seconds longer to run. Therefore, storing the results in a variable rather than using the pipeline caused a 7 percent performance hit. <\/p>\n<p><a href=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/june\/hey0627\/wes-06-27-10-02.jpg\"><img decoding=\"async\" height=\"382\" width=\"604\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0257.wes062710021_54C01BAB.jpg\" alt=\"Image showing results of changed script\" border=\"0\" title=\"Image showing results of changed script\" style=\"border-right-width: 0px;border-top-width: 0px;border-bottom-width: 0px;border-left-width: 0px\" \/> <\/a><\/p>\n<p>Many of the things written about the pipeline are probably referring to Windows PowerShell 1.0, and because of the performance tuning that took place for Windows PowerShell 2.0, they are no longer applicable. However, your environment and your needs may be different. This is where testing comes into play. <\/p>\n<p>If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; &nbsp; Microsoft Scripting Guy Ed Wilson here. I decided to get up early this morning and head out with my laptop, a camera, and my panama hat. My destination? Battery Park. The Scripting Wife and I are in Charleston, South Carolina, for the weekend, and I thought it would be a great time to [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[2,3,4,61,45],"class_list":["post-17931","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-running","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; &nbsp; Microsoft Scripting Guy Ed Wilson here. I decided to get up early this morning and head out with my laptop, a camera, and my panama hat. My destination? Battery Park. The Scripting Wife and I are in Charleston, South Carolina, for the weekend, and I thought it would be a great time to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17931","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17931"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17931\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=17931"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17931"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17931"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}