Using the #Requires statement in PowerShell

Sometimes you have scripts that require specific versions of modules, or if the script is started with Administrator privileges, you can add checks for that in the script itself or… You can use the built-in #Requires statement for that. In this blog post, I will show you how.

What does #Requires do exactly?

“The #Requires statement prevents a script from running unless the PowerShell version, modules (and version), or snap-ins (and version), and edition prerequisites are met. If the prerequisites aren’t met, PowerShell doesn’t run the script or provide other runtime features, such as tab completion.”

Source: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_requires?view=powershell-7.3

How to use #Requires

You can use it in four different ways. Below are the Parameter options with examples.

Note: You can place the #Requires anywhere in the script. For readability, it’s best to insert it at the top of the script, making it more prominent when reading.

Modules

For example, if you need a specific module, ExchangeOnlineManagement, you can use the #Requires -Modules statement. Below is an example that checks if the ExchangeOnlineManagement module is on the system and will give an error if it’s not installed.

#Requires -modules ExchangeOnlineManagement

If the module is on the system, it will return nothing, and the script will continue. If the module is absent, it will throw this error and stop the script from running.

. : The script 'test.ps1' cannot be run because the following modules that are specified by the "#requires" statements of the script are missing: ExchangeOnlineManagement.
At line:1 char:3
+ . 'D:\Temp\test.ps1'
+   ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (test.ps1:String) [], ScriptRequiresException
    + FullyQualifiedErrorId : ScriptRequiresMissingModules

You can also check for the module plus a specific version (Not lower or higher) like this if the module name and version are in a Hashtable format:

#Requires -modules  @{ ModuleName="ExchangeOnlineManagement"; RequiredVersion="3.1.0" }

If the module is present on the system and the version is 3.1.0, it will return nothing, and the script will continue. When the module is on the system but lower than the required version, it will throw an error and stop the script from running.

. : The script 'test.ps1' cannot be run because the following modules that are specified by the "#requires" statements of the script are missing: ExchangeOnlineManagement.
At line:1 char:3
+ . 'D:\Temp\test.ps1'
+   ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (test.ps1:String) [], ScriptRequiresException
    + FullyQualifiedErrorId : ScriptRequiresMissingModules

Other options besides the RequiredVersion are:

  • ModuleVersion: Specify the minimum version of the module
  • MaximumVersion: Specify the maximum version of the module

Note: The module doesn’t need to be imported when checking. It will check if it’s available on the system in one of the module directories.

PSEdition

You can also check whether the script runs in PowerShell Core or the Desktop Edition. Below is an example that checks if the script is started in the Desktop Edition of PowerShell.

#Requires -PSEdition Desktop

If the script is started in the Desktop Edition, it will return nothing, and the script will continue. If the script is run in PowerShell Core, it will throw an error and stop it from running.

. : The script 'test.ps1' cannot be run because it contained a "#requires" statement for PowerShell editions 'Desktop'. The edition of PowerShell that is required by the script does not match the currently running PowerShell Core edition.
At line:1 char:3
+ . 'D:\Temp\test.ps1'
+   ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (test.ps1:String) [], RuntimeException
    + FullyQualifiedErrorId : ScriptRequiresUnmatchedPSEdition

RunAsAdministrator

Probably the most used Parameter, -RunAsAdministrator. Below is an example that checks if the script is started as Administrator.

#Requires -RunAsAdministrator

If the script is started as Administrator, it will return nothing, and the script will continue. If the script is started without Run As Administrator, it will throw an error and stop it from running.

. : The script 'test.ps1' cannot be run because it contains a "#requires" statement for running as Administrator. The current Windows PowerShell session is not running as Administrator. Start Windows PowerShell by  using the Run as 
Administrator option, and then try running the script again.
At line:1 char:3
+ . 'D:\Temp\test.ps1'
+   ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (test.ps1:String) [], ScriptRequiresException
    + FullyQualifiedErrorId : ScriptRequiresElevation

Version

You can also check if the script is started in PowerShell 7 if it requires that, for example. Below is an example that checks if the script is started from within a PowerShell 7 session.

#Requires -Version 7.0

If the script was started from a PowerShell v7 session, it will return nothing, and the script will continue. If the script was started from a PowerShell v5 session, it will throw an error and stop the script from running.

D:\temp\test.ps1 : The script 'test.ps1' cannot be run because it contained a "#requires" statement for Windows PowerShell 7.0. The version of Windows PowerShell that is required by
the script does not match the currently running version of Windows PowerShell 5.1.22621.963.
At line:1 char:1
+ D:\temp\test.ps1
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : ResourceUnavailable: (test.ps1:String) [], ScriptRequiresException
    + FullyQualifiedErrorId : ScriptRequiresUnmatchedPSVersion

More information

You can read more about the #Requires statement here.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.