Skip to content

Instantly share code, notes, and snippets.

@Clickbaitcake
Created December 8, 2020 11:33
Show Gist options
  • Save Clickbaitcake/394d9deef1e5dd664bda121a594dc44a to your computer and use it in GitHub Desktop.
Save Clickbaitcake/394d9deef1e5dd664bda121a594dc44a to your computer and use it in GitHub Desktop.
Checks for the installation of a PowerShell module and tries to install it if its missing.
#Usage: CheckModule -ModuleName ABC123
function CheckModule{
param($ModuleName)
if (Get-Module -ListAvailable -Name $ModuleName) {
Write-Host -ForegroundColor Green "$($ModuleName) exists"
}
else{
Write-Host -ForegroundColor Red "$($ModuleName) does not exist. Attemping install..."
try{
Install-Module $($ModuleName) -Confirm:$False -Force}
catch{
Write-Host -ForegroundColor red "Could not install $($ModuleName) because of $error"}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment