The Import-Module command

PowerShell 3 and later will attempt to automatically load modules if a command from that module is used and the module is under one of the paths in the $env:PSModulePath environment variable. The Import-Module command is less important than it was in Windows PowerShell 2.

For example, if PowerShell is started and the PSDesiredStateConfiguration module is not imported, running the Get-DscResource command will cause the module to be imported. This is shown in the following example:

PS> Get-Module PSDesiredStateConfiguration
PS> Get-DscResource | Out-Null
PS> Get-Module PSDesiredStateConfiguration

ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Manifest 1.1 PSDesiredStateConfiguration ...

In the previous example, the first time Get-Module is executed, the PSDesiredStateConfiguration module has not yet been loaded. After running Get-DscResource, a command from the PSDesiredStateConfiguration module, the module is loaded and the command is immediately executed. Once loaded, the module is visible when running Get-Module.

Modules in PowerShell may be explicitly imported using the Import-Module command. Modules may be imported using a name or with a full path, as shown in the following example:

Import-Module -Name PSWorkflow 
Import-Module -Name C:WindowsSystem32WindowsPowerShellv1.0ModulesPSWorkflowPSWorkflow.psd1 

Once a module has been imported, the commands within the module may be listed using Get-Command as follows:

Get-Command -Module PSWorkflow 
Modules, Get-Command, and auto-loading

As the commands exported by a module can only be identified by importing the module, the previous command will trigger automatic import.

Modules installed in Windows PowerShell 5 and PowerShell Core are placed in a folder named after the module version. This allows multiple versions of the same module to coexist, as shown in the following example:

Version 1.8.1 of PSScriptAnalyzer will be imported by default, as it is the highest version number. It is possible to import a specific version of a module using the MinimumVersion and MaximumVersion parameters:

Import-Module PSScriptAnalyzer -MaxmimumVersion 1.7.0 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.84.155