TEXT

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

Simple Function Do Something

   Use template below to create simple PS function    # Simple Function Do Something # Change $ReportName   function Fu...