Home PowerShell Internals Module How Module Command Discovery Works in PSv3
 
How Module Command Discovery Works in PSv3 – PowerTheShell

How Module Command Discovery Works in PSv3

As you probably know, you can use Import-Module to manually load additional PowerShell modules and get more cmdlets/functions. Beginning in PS V3, though, there is an autoloading mechanism. You no longer need to manually load modules. Instead, PowerShell “knows” the cmdlets/functions that reside in all the modules, offers them in its code completion even if the modules haven’t been loaded yet, and once you use one of these commands, it automatically loads the module. Reason enough to have a deeper look into how this magic works – and why it may be wrong sometimes. Where are my modules?Modules can be located anywhere, and you can use Import-Module with a full path or even an UNC path to load modules from whatever location you want. (you cannot load most binary modules across a network though, i.e. from a mapped drive, etc., due to security restrictions in .NET). For PowerShell to automatically pick up modules and their content, modules must be stored in one of the folders found in the environment variable $env:psmodulepath: $env:PSModulePath -split ‘;’So once a module is stored here, PowerShell can automatically find it and lists the modules when you run: Get-Module -ListAvailableAutodetection of cmdlets/functions found in a module is turned on by default. You can manually turn it off $PSModuleAutoloadingPreference = ‘none’If you do that, though, you now have to manually import any module via Import-Module before you can use any of its cmdlets/functions. You should be careful with changing autoloading behavior because in PowerShell V3, even a lot of internal PowerShell cmdlets … Continue reading How Module Command Discovery Works in PSv3