Need to speed up loading PowerShell modules to run my script sooner!

Hi,

As a newbie, I wrote a short script to run it every time Windows (8.1) starts up. I put it under the "Startup Script" section in 'Group Policy Editor' and in its COMPUTER CONFIGURATION\WINDOWS SETTINGS\SCRIPTS address. The Windows is a live version running from USB Flash Drive; so, it is intended to be run in a variety of systems.

The problem is that in some systems it runs very late which is very critical to me; though, its execution time varies between less than a second and 2-3 seconds (using Measure-Command). I think the process of loading PowerShell modules is a bottleneck and I am looking a fast and affordable way to make that process drastically faster.

I checked also many links such as this "http://blogs.msdn.com/b/powershell/archive/2008/09/02/speeding-up-powershell-startup-updating-update-gac-ps1.aspx?PageIndex=1#comments", but no benefit I got. The script in that link could not be run as it showed some errors in it.

Any help is welcome?

July 29th, 2015 9:08am

Hi Sisili,

if speed is your primary concern, don't script it. Code it in C++.

On the scale you are thinking, all optimizations you can gain from PowerShell will be marginal compared to doing it this way (if you can do it in C++, which is probable).

Cheers,
Fred

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2015 9:16am

Load time is mostly a function of processor and memory. 

How do you know it is module load that is slow?  You have not provided enough info for anyone to be able to help with this.

July 29th, 2015 10:22am

There's not enough detail to tell for sure, but typically when modules are imported for a script there's only one or two cmdlets or functions in the module the script actually uses.  If this is the case, you should be able to cut down on the load time by using the -cmdlet or -function parameters of Import-Module to only load the cmdlets or functions from the module that the script is actually going to use.
Free Windows Admin Tool Kit Click here and download it now
July 29th, 2015 12:28pm

Ahh yes.  Partially loading modules can help but one thing here that is more costly.  If the script is run as a startup script then it will have to wait for the Net Framework to load.  That is going to be last on a boot.  The UI will be up and many other programs will be up before Net is available.

If you can run VBScript and WMI it would be somewhat faster as the WMI and shell are up sooner and both execute very fast on first load.

July 29th, 2015 12:45pm

Also:

$PSModuleAutoLoadingPreference='None'
$PSModuleAutoLoadingPreference='ModuleQualified'

Free Windows Admin Tool Kit Click here and download it now
July 29th, 2015 12:54pm

help Export-Console -full
July 29th, 2015 1:03pm

Hi jrv,

If loading process at startup is something like this ".Net Framework->Powershell Core->MyScript", you are right. My Windows UI is up and my script has not yet run. So, is it worthy to spend some time to use "$PSModuleAutoLoadingPreference='None' " technique to load modules manually in my little script? How could I speed up the above load process by changing, for instance, the 'powershell core' loading mechanism, because it is before the 'automount' feature comes to act when executing powershell scrips?

Thanks

Free Windows Admin Tool Kit Click here and download it now
August 1st, 2015 3:10am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics