A question came up about the behavior of what’s that?).

So What’s the Question?

Does the auto-loading of modules in PowerShell Version 3 use Import-Module behind the scenes?  If it does, can we use default parameter sets to control what types of things are imported?

What Actually Happens?

So, it turns out that default parameter set (new feature) can be used to customize the import of new modules.

$PSDefaultParameterValues = @{ "Import-Module:Alias"=@() "Import-Module:Function"='' "Import-Module:Cmdlet"='' "Import-Module:Variable"='*' } Get-Module Get-BitsTransfer Get-Module

This example (when used in PowerShell V3) will (from a base PowerShell session) show you what modules are loaded, attempt to run the Get-BitsTransfer command, and then show you what modules are now loaded (should include the BitsTransfer module now).&#160; Since we’ve provided an empty array to the Alias parameter for <a href="http://go.microsoft.com/fwlink/?LinkID=141553" target="_blank">Import-Module</a>, any module that is imported will import any aliases.

### Seems Like It Works… Where’s the Problem?

The problem comes in when you specify that only one of the four parameters (Alias, Function, Cmdlet, and Variable).

You might expect that when you specify one parameter (example Alias), that the other portions of the import (Function, Cmdlet, and Variable) would behave just like if you did not specify any of them. 

Example -

Open a new PowerShell session (V2 or V3) -

Import-Module BitsTransfer
Get-Command -Module BitsTransfer

This will import BitsTransfer module and all attendant commands and functions.&#160; Note the ExportedCommands in the Get-Module output.

Open a new PowerShell session and try -

Import-Module BitsTransfer -Alias @() Get-Command -Module BitsTransfer


This should import the BitsTransfer without any aliases (yes, I know BitsTransfer doesn’t have any aliases, but I wanted a module that most any Windows 7 or Server 2008 R2 machine would have – feel free to test with whatever module you desire). 

This did import the BitsTransfer, but no commands or variables were imported.  It only attempted to import aliases, and since we specified that aliases were to be an empty array, nothing was imported.

This is counter-intuitive and is not the experience we should have.  Specifying one parameter should not change the default value of other parameters (from all to none in this case).

What Can I Do About It?

First of all, open PowerShell and try it!  If you have one of the CTPs of Version 3, try it there.  Also try it in V2 using Import-Module directly.

Then go to Microsoft Connect and vote up https://connect.microsoft.com/PowerShell/feedback/details/716857/module-partially-loads-with-import-module.

Want more great reading? Check out my reading list!