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:\><strong> get-module</strong> 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:

 

<code>PS C:\><strong> Send-Wakeup savdalhv01</strong></code>

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:\> <strong>get-module</strong> 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?"

Please or Register to post comments.

Latest from the Blog
May 2, 2013
blog

Flash Storage Arrays and the Need for Speed

Flash storage has some tremendous performance advantages over traditional rotational drives. Recent advances in the capacities and corresponding reduction in costs of flash storage have opened up many new flash based enterprise options....More
ODX Windows Server 2012
Apr. 24, 2013
Sponsored

Digging Into Windows Server 2012 New Storage Features

Windows Server 2012 has several new storage features that help address today’s storage challenges and bring some of the capabilities that were traditionally only offered by SANs within the reach of smaller businesses, while at the same time offering improved integration with SAN storage....More
Mar. 29, 2013
Sponsored

Going Beyond Virtualization with the Private Cloud

Learn how the private cloud extends beyond virtualization...More
Mar. 25, 2013
Sponsored

Mission Critical Application Virtualization 1

Virtualization has become a key IT infrastructure technology. Its ability to facilitate server consolidation increases your hardware ROI while simultaneously reducing your management and infrastructure requirements....More

Are you using Virtual Desktop Infrastructure (VDI) technologies?