Monday, May 9, 2016

PowerShell v2 Import Module Importing Multiple Modules

,
To solve some scripting issues I have been working with the Import-Module cmdlet a little more this morning.  As the help says,
Get-Help Import-Module -Parameter Name

-Name <string[]>
Specifies the names of the modules to import. Enter the name of the module or the name of a file in the module, such as a .psd1, .psm1, .dll, or ps1 file. File paths are optional. Wildcards are not permitted. You can also pipe module names and file names to Import-Module.

If you omit a path, Import-Module looks for the module in the paths saved in the PSModulePath environment variable ($env:PSModulePath).

Specify only the module name whenever possible. When you specify a file name, only the members that are implemented in that file are imported. If the module contains other files, they are not imported, and you might be missing important members of the module.

Required? true
Position? 1
Default value
Accept pipeline input? true (ByValue)
Accept wildcard characters? false
the -Name parameter takes an array of strings. So, I can use this approach:
Import-Module ServerManager,WebAdministration
but, I wanted it to be a little more dynamic than that in case I have a long list of modules to import or a dynamic set entered at execution time. My first efforts didnt work:
$modulelist = @("ServerManager","WebAdministration")
Import-Module ($modulelist -join ,)
Nor did my second:
$modulelist = @("ServerManager","WebAdministration")
Import-Module $($modulelist -join ,)
So, I went back to simple just to be sure it would work,
Import-Module ServerManager,WebAdministration
and it did. So, I tried just passing the array,
$modulelist = @("ServerManager","WebAdministration")
Import-Module $modulelist
and that proved to be the perfect approach.

Conclusion: if you want to load an array of module names with Import-Module just pass it an array of strings.

0 comments to “PowerShell v2 Import Module Importing Multiple Modules”

Post a Comment

 

Computer Info Copyright © 2016 -- Powered by Blogger