I have a Windows 2008 R2 server with Powershell V4.0

I got a very simple script test.ps1 with this content :

Write-host "Hello world!"

I call this script from a powershell prompt like this :

measure-command { powershell.exe -file .\test.ps1 }

I got extremely high value as you can see :

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 7
Milliseconds      : 992
Ticks             : 79922815
TotalDays         : 9,25032581018519E-05
TotalHours        : 0,00222007819444444
TotalMinutes      : 0,133204691666667
TotalSeconds      : 7,9922815
TotalMilliseconds : 7992,2815

Or

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 4
Milliseconds      : 655
Ticks             : 46556579
TotalDays         : 5,38849293981481E-05
TotalHours        : 0,00129323830555556
TotalMinutes      : 0,0775942983333333
TotalSeconds      : 4,6556579
TotalMilliseconds : 4655,6579

And it taking my cpu at 50%. Why it's too long ? I tried with -noprofile but it's same.

Thanks for your help.

up vote 6 down vote accepted

Powershell loads and JIT compiles tons, and tons, and tons of stuff. This takes a lot of excessive CPU cycles and disk I/O.

Try using ngen.exe (Native Image Generator) to precompile the assemblies that Powershell loads during startup. Just might improve your startup time.

$FrameworkDir=[Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()
$NGENPath = Join-Path $FrameworkDir 'ngen.exe'

[AppDomain]::CurrentDomain.GetAssemblies() |
  Select-Object -ExpandProperty Location |
  ForEach-Object { & $NGENPath """$_""" } 

Read about ngen here.

  • Resolved! Thank you – Adeel ASIF Mar 4 '16 at 7:32

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.