PowerShell modules are units of reusable, distributable, and script-based code that can be used to encapsulate and share functionality in PowerShell. Modules allow you to write and organize scripts in a consistent and maintainable way, and to easily reuse them in other scripts and projects. Modules can contain functions, cmdlets, providers, and workflows, and they can be written in PowerShell or in other languages, such as C#.
To create a module in PowerShell, you can use the New-Module
cmdlet or create a script file with a .psm1
extension. You can then import the module into your PowerShell session using the Import-Module
cmdlet or you can import it automatically by placing it in one of the directories specified in the PSModulePath
environment variable.
More: How to Run PowerShell from C#
Modules are an important tool for organizing and sharing code in PowerShell, and they are widely used in both interactive shell sessions and automated scripts.
Install PowerShell Module
To install a PowerShell module, you can use the Install-Module
cmdlet. This cmdlet is available in PowerShell 5.0 and newer, and it allows you to install modules from the PowerShell Gallery or from a file path on your local machine.
Here is an example of how to use the Install-Module
the cmdlet to install a module from the PowerShell Gallery:
Install-Module -Name MyModule
This will install the module with the name “MyModule” from the PowerShell Gallery.
You can also specify a particular version of the module to install by using the -RequiredVersion
parameter:
Install-Module -Name MyModule -RequiredVersion 1.2.3
To install a module from a file path on your local machine, you can use the -Path
parameter:
Install-Module -Path C:\path\to\module\MyModule.psm1
By default, the Install-Module
cmdlet installs modules to the current user’s profile, but you can use the -Scope
parameter to specify a different installation location, such as AllUsers
or LocalMachine
.
For more information on the Install-Module
cmdlet and other ways to install PowerShell modules, you can refer to the PowerShell documentation.
Delete PowerShell Module
To delete a PowerShell module, you can use the Remove-Module
cmdlet. This cmdlet removes the module and its contents from the current session, or from the specified location in the module path.
Here is an example of how to use the Remove-Module
the cmdlet to delete a module from the current session:
Remove-Module -Name MyModule
This will remove the module with the name “MyModule” from the current session.
You can also specify a particular version of the module to delete by using the -RequiredVersion
parameter:
Remove-Module -Name MyModule -RequiredVersion 1.2.3
To delete a module from a specific location in the module path, you can use the -Path
parameter:
Remove-Module -Path C:\path\to\module\MyModule.psm1
By default, the Remove-Module
cmdlet only removes the module from the current session, but you can use the -Force
parameter to remove the module from the module path as well.
For more information on the Remove-Module
cmdlet and other ways to delete PowerShell modules, you can refer to the PowerShell documentation.