Tuesday, January 24, 2017

Dot-sourcing can be slower than reading in and then evaluating

Someone had a ton of PowerShell functions to declare and for version control reasons put them each in a separate file. They found that dot-sourcing the files to run the script and include the functions in the current scope took notably longer than opening each script file and invoking the contents like so:

dir *.ps1 | % { iex (gc $_.FullName -Raw) }

Understandably, they were curious about this. I did some investigation (profiling) and found that dot-sourcing eventually involves a call to WinVerifyTrust, apparently to verify some resource's integrity. This is probably to look for a digital signature on the script files, even when the execution policy allows running unsigned scripts. This doesn't happen when getting the script text manually and then invoking it.

So, if you're running a ton of scripts, you might consider using Get-Content and Invoke-Expression.

Based on my Super User answer.

No comments:

Post a Comment