Sunday, January 5, 2020

Check If Module Exist



Here is simple PowerShell script to check if any given module exists or not. We will be using try and catch block to determine the existence of AzureAD module


# Check If module exist

$module = 'AzureAD'
try {
    Import-Module $module -ErrorAction stop
    Write-Host 'Module exists' -f Green
}
catch {
    Write-Host 'Module does not exist' -f red -b Black
}



There are other ways to accomplish same results. One other way of doing it would be




#(1)_.Assign Vars
$checkModule = 'MsOnline'
$List = (Get-InstalledModule).name

#(2)_.Check if Module Installed
if ($List -contains $checkModule)
{

   Write-host 'Module is installed' -f Green -$checkModule

}

else {

    Write-host 'Module is not installed' -f Red -$checkModule

}




Another way of checking doing same check;

#(3)_.Check if Module Installed
if (!($List -contains $checkModule))
{
   Write-host 'Module is not installed' -f Red -$checkModule
  

}

else {

   Write-host 'Module is installed' -f Green -$checkModule

}



Casey DeDeal
Azure Certified Solutions Architect
AWS Certified Cloud Practitioner

https://msazure365.blogspot.com
https://simplepowershell.blogspot.com
https://twitter.com/Message_Talk




No comments:

Post a Comment

Connect-Azure-PowerShell if there is no existing Connection.

By Using MsolService to connect to Azure PowerShell, could be annoying time to time if unable to validate existing connection. The simpl...