Import-Module in Try Catch

I have a machine with the modules AzureAD and Az installed.

Executing Import-Module AzureAD or Import-Module Az both work OK from within Powershell ISE.

When putting Import-Module AzureAD inside a Try Catch it still works, however putting Import-Module Az inside a Try Catch blocks the ISE (I have to close it or end the statement with Ctrl-C).

My question is: what can cause the import to fail in this way? Why only with the Az module and why only inside a Try Catch?

Not sure how a try\catch would cause any issue. Would recommend removing the need to wrap try\catch using #Requires

#Requires -Modules AzureAD, Az

Import-Module -Name AzureAD
Import-Module -Name Az

Do not have the Az module installed, so:

PS C:\Users\rasim> c:\Users\rasim\Desktop\temp.ps1
ResourceUnavailable: The script 'temp.ps1' cannot be run because the following modules that are specified by the "#requires" statements of the script are missing: Az.

Hello Rob,

Thanks for the suggestion. The problem is that I need to be able to react when the module is not yet installed. The #Requires statement prevents the script from running if the module isn’t there.

But thanks anyway,