Skip to content

Instantly share code, notes, and snippets.

@jaloplo
Last active March 3, 2024 18:34
Show Gist options
  • Save jaloplo/362f9d79479c3998f680ecf92865616a to your computer and use it in GitHub Desktop.
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.
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