Install-ModuleIfNotPresent.ps1
19 Sep 2025Description
Purpose
Installs a PowerShell module if it is not already present and imports it.
Detailed Description
The Install-ModuleIfNotPresent function checks if a specified PowerShell module is already installed. If the module is installed, it imports the module. If the module is not installed, it installs the module from a specified repository, and then imports the module.
Usage
Example 1
Install-ModuleIfNotPresent -ModuleName "AzureRM" -Repository "PSGallery"
This example installs the “AzureRM” module from the “PSGallery” repository if it is not already installed, and then imports the module.
Notes
Author: Your Name Date: Today’s Date
Script
<#
.SYNOPSIS
Installs a PowerShell module if it is not already present and imports it.
.DESCRIPTION
The Install-ModuleIfNotPresent function checks if a specified PowerShell module is already installed. If the module is installed, it imports the module. If the module is not installed, it installs the module from a specified repository, and then imports the module.
.PARAMETER ModuleName
The name of the PowerShell module to install and import.
.PARAMETER Repository
The repository from which to install the PowerShell module.
.EXAMPLE
Install-ModuleIfNotPresent -ModuleName "AzureRM" -Repository "PSGallery"
This example installs the "AzureRM" module from the "PSGallery" repository if it is not already installed, and then imports the module.
.INPUTS
None.
.OUTPUTS
None.
.NOTES
Author: Your Name
Date: Today's Date
#>
function Install-ModuleIfNotPresent {
param(
[Parameter(Mandatory = $true)]
[string]$ModuleName,
[Parameter(Mandatory = $true)]
[string]$Repository
)
try {
if ((Get-Module -Name $ModuleName -ListAvailable)) {
Write-Verbose "Importing module - $($ModuleName)"
Import-Module -Name $ModuleName
}
Else {
Write-Verbose "Installing module - $($ModuleName)"
Install-Module -Name $ModuleName -Repository $Repository -Force -ErrorAction Stop
Import-Module -Name $ModuleName
}
}
catch {
Write-Error -Message $_.Exception.Message
}
}
Download
Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.
Report Issues
You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.