By default, on Windows 10 and higher, that location is $HOME\Documents\PowerShell\Modules . On Linux or Mac, the CurrentUser location is $HOME/. local/share/powershell/Modules .
At the PowerShell prompt, type Get-Module -ListAvailable to see which modules are active. To install a module, use the Get-InstalledModule cmdlet.
Create a PSSession on the remote computer and then use the PSSession parameter of Get-Module to get the PowerShell modules in the remote session. When you import a module from the remote session the imported commands run in the session on the remote computer.
Use CommandType or its alias, Type. By default, Get-Command gets all cmdlets, functions, and aliases. The acceptable values for this parameter are: Alias : Gets the aliases of all PowerShell commands.
You can use the ListAvailable
option of Get-Module
:
if (Get-Module -ListAvailable -Name SomeModule) {
Write-Host "Module exists"
}
else {
Write-Host "Module does not exist"
}
The ListAvailable option doesn't work for me. Instead this does:
if (-not (Get-Module -Name "<moduleNameHere>")) {
# module is not loaded
}
Or, to be more succinct:
if (!(Get-Module "<moduleNameHere>")) {
# module is not loaded
}
A module could be in the following states:
If you just want to have the darn thing available in a PowerShell session for use, here is a function that will do that or exit out if it cannot get it done:
function Load-Module ($m) {
# If module is imported say that and do nothing
if (Get-Module | Where-Object {$_.Name -eq $m}) {
write-host "Module $m is already imported."
}
else {
# If module is not imported, but available on disk then import
if (Get-Module -ListAvailable | Where-Object {$_.Name -eq $m}) {
Import-Module $m -Verbose
}
else {
# If module is not imported, not available on disk, but is in online gallery then install and import
if (Find-Module -Name $m | Where-Object {$_.Name -eq $m}) {
Install-Module -Name $m -Force -Verbose -Scope CurrentUser
Import-Module $m -Verbose
}
else {
# If the module is not imported, not available and not in the online gallery then abort
write-host "Module $m not imported, not available and not in an online gallery, exiting."
EXIT 1
}
}
}
}
Load-Module "ModuleName" # Use "PoshRSJob" to test it out
The current version of Powershell has a Get-InstalledModule
function that suits this purpose well (or at least it did in my case).
Get-InstalledModule
Description
The
Get-InstalledModule
cmdlet gets PowerShell modules that are installed on a computer.
The only issue with it is that it throws an exception if the module that is being requested doesn't exist, so we need to set ErrorAction
appropriately to suppress that case.
if ((Get-InstalledModule `
-Name "AzureRm.Profile" `
-MinimumVersion 5.0 ` # Optionally specify minimum version to have
-ErrorAction SilentlyContinue) -eq $null) {
# Install it...
}
You can use the Get-InstalledModule
If (-not(Get-InstalledModule SomeModule -ErrorAction silentlycontinue)) {
Write-Host "Module does not exist"
}
Else {
Write-Host "Module exists"
}
When I use non-default modules in my scripts I call the function below. Besides the module name, you can provide a minimum version.
# See https://www.powershellgallery.com/ for module and version info
Function Install-ModuleIfNotInstalled(
[string] [Parameter(Mandatory = $true)] $moduleName,
[string] $minimalVersion
) {
$module = Get-Module -Name $moduleName -ListAvailable |`
Where-Object { $null -eq $minimalVersion -or $minimalVersion -lt $_.Version } |`
Select-Object -Last 1
if ($null -ne $module) {
Write-Verbose ('Module {0} (v{1}) is available.' -f $moduleName, $module.Version)
}
else {
Import-Module -Name 'PowershellGet'
$installedModule = Get-InstalledModule -Name $moduleName -ErrorAction SilentlyContinue
if ($null -ne $installedModule) {
Write-Verbose ('Module [{0}] (v {1}) is installed.' -f $moduleName, $installedModule.Version)
}
if ($null -eq $installedModule -or ($null -ne $minimalVersion -and $installedModule.Version -lt $minimalVersion)) {
Write-Verbose ('Module {0} min.vers {1}: not installed; check if nuget v2.8.5.201 or later is installed.' -f $moduleName, $minimalVersion)
#First check if package provider NuGet is installed. Incase an older version is installed the required version is installed explicitly
if ((Get-PackageProvider -Name NuGet -Force).Version -lt '2.8.5.201') {
Write-Warning ('Module {0} min.vers {1}: Install nuget!' -f $moduleName, $minimalVersion)
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Scope CurrentUser -Force
}
$optionalArgs = New-Object -TypeName Hashtable
if ($null -ne $minimalVersion) {
$optionalArgs['RequiredVersion'] = $minimalVersion
}
Write-Warning ('Install module {0} (version [{1}]) within scope of the current user.' -f $moduleName, $minimalVersion)
Install-Module -Name $moduleName @optionalArgs -Scope CurrentUser -Force -Verbose
}
}
}
usage example:
Install-ModuleIfNotInstalled 'CosmosDB' '2.1.3.528'
Please let me known if it's useful (or not)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With