Skip to content

Instantly share code, notes, and snippets.

@jaloplo
Last active March 3, 2024 18:34
Show Gist options
  • Save jaloplo/dd931ec911448c4e5060b245ccb8470f to your computer and use it in GitHub Desktop.
Save jaloplo/dd931ec911448c4e5060b245ccb8470f to your computer and use it in GitHub Desktop.
PowerShell script to import in the current the latest version published of the module including prerelease ones. It installs the module if it is not.
param(
[Parameter(Mandatory=$true)]
[string]$ModuleName,
[switch]$Force
)
Write-Host "Checking if module '$ModuleName' is installed..."
$moduleInstalled = Get-Module -Name $ModuleName -ListAvailable
if (-not $moduleInstalled) {
Write-Host "Module '$ModuleName' is not installed. Installing..."
# Install module
Install-Module -Name $ModuleName -Force -AllowClobber -AllowPrerelease -SkipPublisherCheck
}
elseif ($true -eq $Force) {
Write-Host "Installing..."
# Install PnP PowerShell module
Install-Module -Name $ModuleName -Force -AllowClobber -AllowPrerelease -SkipPublisherCheck
}
# Check if module is imported, and import if not
$moduleImported = Get-Module -Name $ModuleName -ErrorAction SilentlyContinue
if (-not $moduleImported) {
Write-Host "Module '$ModuleName' is not imported. Importing..."
Import-Module $ModuleName -Force
}
elseif ($true -eq $Force) {
Write-Host "Importing..."
Import-Module $ModuleName -Force
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment