Site icon Think PowerShell

Working with PowerShell modules

PowerShell modules are packages that contain PowerShell functions, cmdlets, and variables. These components are grouped together based on functionality and can be imported into PowerShell to extend its capabilities. In this article, we’ll take a closer look at PowerShell modules, why they’re essential, and how to work with them.

Importing and Using Modules

Understanding PowerShell modules

A PowerShell module is essentially a set of related functions, cmdlets, aliases, and variables saved as a .psm1 file. It allows you to group together related functionalities and distribute them as a single unit.

For instance, the ActiveDirectory module contains cmdlets for managing Active Directory, and the SqlServer module contains cmdlets for managing SQL Server.

Importing and using modules

To use a module, you need to import it into your PowerShell session with the Import-Module cmdlet.

# Import the ActiveDirectory module
Import-Module ActiveDirectory

Once a module is imported, you can use its cmdlets, functions, and variables as if they were part of PowerShell.

# Use a cmdlet from the ActiveDirectory module
Get-ADUser -Filter *

Discovering modules

You can find out what modules are installed on your system with the Get-Module cmdlet.

# List all available modules
Get-Module -ListAvailable

Installing new modules

New modules can be downloaded and installed from the PowerShell Gallery using the Install-Module cmdlet.

# Install the SqlServer module
Install-Module -Name SqlServer

Exporting functions in modules

When you’re creating your own modules, you can control what functions are exported (i.e., made available to the user) using the Export-ModuleMember cmdlet.

# Inside your .psm1 file
function Get-InternalFunction {
    # Not available to the user
}

function Get-ExternalFunction {
    # Available to the user
}

Export-ModuleMember -Function Get-ExternalFunction

Best practices

More resources

Exit mobile version