PowerShell Module Automatic Load in Windows 8 and Server 2012

In Windows 8 and Windows Server 2012, PowerShell cmdlets will work even though you might not have loaded that module.

John Savill

January 20, 2013

2 Min Read
PowerShell Module Automatic Load in Windows 8 and Server 2012

Q: Why, even though no module was loaded in my Windows 8 and Windows Server 2012 PowerShell session, did a cmdlet from the module still work?

A: Typically to use cmdlets from a module, that module must be imported into the current PowerShell session using the Import-Module cmdlet. And that's still a good practice to explicitly load the modules you need in your session. However, PowerShell 3.0, which is part of Windows 8 and Windows Server 2012 (but is also available for older OSs) features a new auto-loading capability of modules.

This is likely what you are seeing if cmdlets from modules not loaded "just work". The new functionality works by automatically scanning all the modules in your default path for commands.

If it comes across a command it doesn't know, and if it finds a match, then that module is automatically loaded and used. This also works for custom scripted modules (psm1) you may have created. Below is this behavior in action using my own Send-Wakeup function I mentioned in a previous FAQ.

First, I look at the modules I have loaded.

PS C:> get-module ModuleType Name ExportedCommands ---------- ---- ---------------- Script ISE {Get-IseSnippet, Import-IseSnippet, New-IseSnippet} Manifest Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...} Manifest Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...} 

I can see I have three modules loaded.

Now I try to run a cmdlet from a module I haven't loaded:

 

PS C:> Send-Wakeup savdalhv01


Wake-On-Lan magic packet sent to 00:15:17:C4:E1:05, length 102

It just worked. If I look again at the modules loaded, I see my custom module was automatically loaded when my PowerShell session found I didn'tt have a module loaded with the command I entered. It went out and found a match and loaded it automatically.

PS C:> get-module ModuleType Name ExportedCommands ---------- ---- ---------------- Script ISE {Get-IseSnippet, Import-IseSnippet, New-IseSnippet} Manifest Microsoft.PowerShell.Management {Add-Computer, Add-Content, Checkpoint-Computer, Clear-Content...} Manifest Microsoft.PowerShell.Utility {Add-Member, Add-Type, Clear-Variable, Compare-Object...} Script WakeUp Send-Wakeup 

This demonstrates the new autoimport functionality in PowerShell 3.0. However, I still think it's a better option to manually load the modules you want to use, as it gives you more control over the actual module being used. Sometimes it might happen that two different modules could have a cmdlet with the same name and might therefore give unexpected results.

You might also look at "Q: How can I have a certain PowerShell module always load when I start PowerShell?"

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like