#Requires -Module - am i understanding?

Good day,

I have a function that i would like to not load if WebAdministration module is not present as it relies on that being installed as part of IIS.


At the top of the function I have added

#Requires -Module WebAdministration

However it appears to load and partially load before it hits a trap i have within the function then it exits.

Am i wrong of shouldn’t the require statement perform a check at load time for the specified module, if not found close? We are on PS 5.1 currently.


Thank you for any clarification i may miss.

As I understand it according to the help about_Requires it prevents a script - not a function - from running if the requirement does not match the environment it is about to run in.

Another way could be to include it in a module and use the RequiredModules” statement in the module manifest to prevent the module to load if the desired module isn’t available.

I had thought along those same lines as well Olaf, however the following line in the article you linked states:

Placing a #Requires statement inside a function does NOT limit its scope.

Which led me to believe it could be used in a function.

Also after further re-reading using -Module will attempt to install the module and throw a terminating error if it is unable. I had considered adding requires in my module manifest as well, however there were modules that did not require that module and i wanted them to function as normal.

After further consideration I believe my best option is to try/catch import-module webadministration, if the error is file not found exit gracefully without throwing error but if anything else throw error.


I really appreciate your quick reply and assistance.


Have a wonderful day.

Another option would be this:

if(Get-Module -ListAvailable -Name WebAdministration){‘cool’}else{‘uncool’}