Modules in PowerShell

A module is a package that contains PowerShell members, such as cmdlets, providers, functions, workflows, variables, and aliases. The members of this package can be implemented in a PowerShell script, a compiled DLL, or a combination of both. These files are usually grouped together in a single directory.

Beginning in PowerShell 3.0, PowerShell imports modules automatically the first time that you run any command in an installed module. You can now use the commands in a module without any set-up or profile configuration, so there's no need to manage modules after you install them on your computer.

The Get-Command cmdlet gets all commands in all installed modules, even if they are not yet in the session.

Get-Command -Module SimplySql

Only modules that are stored in the location specified by the PSModulePath environment variable are automatically imported. Modules in other locations must be imported by running the Import-Module cmdlet.

Also, commands that use PowerShell providers do not automatically import a module. For example, if you use a command that requires the WSMan: drive, such as the Get-PSSessionConfiguration cmdlet, you might need to run the Import-Module cmdlet to import the Microsoft.WSMan.Management module that includes the WSMan: drive.

You can still run the Import-Module command to import a module and use the $PSModuleAutoloadingPreference variable to enable, disable and configure automatic importing of modules.

How to Use a Module

Literal strings are strings that doesn't evaluate variables and special characters. It's created using single quotes.

To use a module, perform the following tasks:

How to Install a Module

If you receive a module as a folder with files in it, you need to install it on your computer before you can use it in PowerShell.

Most modules are installed for you. PowerShell comes with several preinstalled modules, sometimes called the core modules.

Use the following command to create a Modules directory for the current user:

New-Item -Type Directory -Path $HOME\Documents\PowerShell\Modules

Copy the entire module folder into the Modules directory. You can use any method to copy the folder, including Windows Explorer and Cmd.exe, as well as PowerShell. In PowerShell use the Copy-Item cmdlet.

Copy-Item -Path C:\ps-test\MyModule -Destination ` $HOME\Documents\PowerShell\Modules

How to Find Installed Modules

To find modules that are installed in a default module location, but not yet imported into your session, type:

Get-Module -ListAvailable #To find the modules that have already been imported into your session Get-Module

How to Find the Commands in a Module

Use the Get-Command cmdlet to find all available commands

Get-Command -Module module-name

How to Import a Module

You might have to import a module or import a module file. Importing is required when a module is not installed in the locations specified by the PSModulePath environment variable, $env:PSModulePath, or the module consists of file, such as a .dll or .psm1 file, instead of typical module that is delivered as a folder.

Import-Module module-name #To add the TestCmdlets module in the C:\ps-test directory to your session Import-Module C:\ps-test\TestCmdlets #To add the TestCmdlets.dll module in the C:\ps-test directory to your session Import-Module C:\ps-test\TestCmdlets.dll

How to Import a Module into Every Session

The Import-Module command imports modules into your current PowerShell session. To import a module into every PowerShell session that you start, add the Import-Module command to your PowerShell profile.

How to Remove a Module

To remove a module from your session, use the following command format.

Remove-Module module-name

PSModulePath

The $env:PSModulePath environment variable contains a list of folder locations that are searched to find modules and resources.

$env:PSModulePath C:\Users\Sagar\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPower Shell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules