Use PowerShell
The Shell Is Calling
The Shell Is Calling
A question came up about the behavior of Import-Module (in the context of this interesting discussion of whether module authors should provide aliases), especially with Version 3 and the auto-loading of modules (what’s that?).
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?
So, it turns out that Import-Module does get called behind the scenes and a 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). Since we’ve provided an empty array to the Alias parameter for Import-Module, any module that is imported will import any aliases.
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 Get-Module
This will import BitsTransfer module and all attendant commands and functions. Note the ExportedCommands in the Get-Module output.
Open a new PowerShell session and try -
Import-Module BitsTransfer -Alias @() Get-Command -Module BitsTransfer Get-Module
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).
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.
January 6, 2012 - 11:37 am
Hey Steve, I thought of another good test to try out with this, but too busy right now to test it. If module auto-load is enabled, and if modules auto-load when you use aliases, then I suspect if you configure PowerShell this way that you would be able to auto-load a module using an alias only to then get a command not found error. I suppose this makes sense, because the command indeed would not be found, but it’s an interesting use case because you end up changing the system state (loading a module) with an invalid command (according to how you configured modules to load: without aliases). The default parameter values feature will probably reveal a bunch of unusual scenarios like this.