PowerShell with a Purpose Blog

PowerShell v3: Autoloading Modules

This is about PowerShell v3, which is now available with the Windows 8 Developer Preview and as a standalone Community Technology Preview. As such, mycaveats about it apply.

I hate getting "command not found" errors in PowerShell. It's typically because I've opened a new PowerShell window, forgotten to load a module, and tried to run a command from that module. My bad, but annoying.

Windows 8 promises to make the problem worse. The Server version of the OS will ship with more than 80 modules. Am I supposed to just pop them all into a profile, so that PowerShell takes forever to start up? Or am I supposed to remember which cmdlet came from which module, and load them all manually?

As it turns out, neither. PowerShell v3 supports automatic cmdlet discovery and module loading. Essentially, it works like this: Commands like Get-Command will show not only loaded cmdlets, but also cmdlets that are accessible from any unloaded modules located in a folder that's part of the PSModulePath environment variable. That's important, because it means the shell's most important "discovery" mechanisms will be showing you what cmdlets are actually available - even if you haven't loaded them yet! Try to run one of those cmdlets, and PowerShell will automatically load the necessary module "just in time." Cool!

This behavior can be disabled, of course, through a built-in variable (check the VARIABLE: drive after installing v3 and you'll find it easily enough), but module autoloading promises to make cmdlets easier to run and easier to find.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish