Last active
March 3, 2024 18:34
-
-
Save jaloplo/362f9d79479c3998f680ecf92865616a to your computer and use it in GitHub Desktop.
PowerShell script to import a module in the current session. It installs the module if it is not.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter(Mandatory=$true)] | |
[string]$ModuleName | |
) | |
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 | |
} | |
# 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 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment