I removed the outer quotes cause otherwise the command is just a string,
PowerShell won't execute it (unless invoked):
PS > $fc = 'cmd /c dir c:\windows /b /s | findstr /r "\.txt$ .\log$" '
PS > $fc
cmd /c dir c:\windows /b /s | findstr /r "\.txt$ .\log$"
PS > measure-command {$fc = dir c:\windows -r -inc *.txt,*.log}
(...)
Seconds : 32
Milliseconds : 167
Ticks : 321679677
TotalDays : 0.000372314440972222
TotalHours : 0.00893554658333333
TotalMinutes : 0.536132795
TotalSeconds : 32.1679677
TotalMilliseconds : 32167.9677
PS > measure-command {$fc = iex 'cmd /c dir c:\windows /b /s | findstr /r
"\.txt$ .\log$" '}
(...)
Seconds : 25
Milliseconds : 29
Ticks : 250297940
TotalDays : 0.000289696689814815
TotalHours : 0.00695272055555556
TotalMinutes : 0.417163233333333
TotalSeconds : 25.029794
TotalMilliseconds : 25029.794
When invoked, the cmd expression is faster but it leaves you with raw strings
that you need to parse/concat and then use get-childitem to turn them into
full .net objects, and all of that can eventually take the precious seconds
we are trying to save. Which one is better/elegant? That's for the readers
to decide
---
Shay Levy
Windows PowerShell MVP
blog:
http://blogs.microsoft.co.il/blogs/ScriptFanatic
A> I found the cause of the slower processing. You unwrapped the
A> pipeline; it's the difference between this
A>
A> $fc = 'cmd /c dir c:\windows /b /s | findstr /r "\.txt$ .\log$" '
A>
A> and this:
A>
A> $fc = cmd /c dir /s /b c:\windows | findstr /r "\.txt$ \.log$"
A>
A> The external single-quotes ensure that the piping into findstr is
A> part of cmd.exe's job, rather than PowerShell's, in the interest of
A> speeding up the initial processing.
A>
A> Your test also confirms something else I suspected but wasn't sure
A> about. Once you've created pipeline objects, processing them with
A> non-PowerShell tools probably doesn't give any significant
A> performance benefits, simply because of the cost of flattening them
A> into textstream objects and then reconstituting them.
A>
A> "Shay Levy [MVP]" <no@xxxxxx> wrote in message
A> news:89228ed2383d38cabbaf7e6b2162@xxxxxx
A>
>> PS > measure-command {cmd /c dir /s /b c:\windows | findstr /r
>> "\.txt$ \.log$"}
>>