Speed up PowerShell scripts in V2?
Apart from minimizing what you add to your various profile scripts (shown below), there isn't much you can do:
C:\PS> $profile | fl * -force
AllUsersAllHosts : C:\Windows\System32\WindowsPowerShell\v1.0\profile.ps1
AllUsersCurrentHost : C:\Windows\System32\WindowsPowerShell\v1.0\Microsoft.PowerShell_profile.ps1
CurrentUserAllHosts : C:\Users\hillr\Documents\WindowsPowerShell\profile.ps1
CurrentUserCurrentHost : C:\Users\hillr\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
One way to check if the scripts caused latency profiles is to start powershell with the -noprofile option. If the startup time is different, it is due to your profile scripts. You can use .NET stopwatch like this:
function TimeThis([scriptblock]$scriptblock, $msg)
{
if (!$stopWatch)
{
$script:stopWatch = new-object System.Diagnostics.StopWatch
}
$stopWatch.Reset()
$stopWatch.Start()
. $scriptblock
$stopWatch.Stop()
if ($msg -eq $null) { $msg = "$scriptblock" }
"Execution time: $($stopWatch.ElapsedMilliseconds) mS for $msg"
}
. TimeThis {Import-Module $Module -args ~\Pscx.UserPreferences.ps1}
As long as you can use Measure-Command it doesn't show what is being executed and you don't get any command output (only the timing is very verbose).
There used to be an issue in earlier CTPs where the installer did not build PowerShell assemblies and this could cause noticeable load time delays. However, I'm pretty sure this is fixed with final install 2.0 (and of course PowerShell built into Windows 7 and Windows Server 2008 R2). If the following directory and its contents exist, you should be ngen'd:
dir 'C:\Windows\assembly\NativeImages_v2.0.50727_32\Microsoft.PowerShel#' -r
a source to share
Tracking down a performance issue as it can be tricky, but there are a few things you can do to improve / fix it.
First of all, PowerShell starts cold or warm. At least on my workstation, when I first start PS in the morning, it takes a little longer to start up than later ones. Is there a way you can keep it warm to minimize load times?
Second, use the Process Monitor tool from the great folks at Windows Sysinternals . Install it to monitor the powershell.exe process and see what it does which is taking so long. For me, I have a few mapped network drives and common scripts that come from the network. In my testing, I measured about two seconds of latency when running on a remote script I was downloading.
PowerShell needs to load a bunch of resources from disk into memory, so it goes without saying that optimizing your I / O will help as well. Defrag, make sure you have enough free memory, etc. It even queries the registry a little, so you can make sure the registry is completely defragmented - although this is a rather long snapshot.
a source to share
It may be completely unrelated, but I once had a huge pause while starting PowerShell.
Was somehow related to my laptop being on the domain, then I went nuts and started over again when the laptop was no longer on the domain. I looked at a startup with dottrace and could only see that the code was stuck somewhere in the initialization of Providers.
Restarting the machine helped in this case. It also doesn't always happen (in fact, only once).
a source to share