Script errors when running on PowerShell 5.1 with requires module statement, but not PowerShell 7

43 views Asked by At

I have written a PowerShell Module that interacts with an API of a system we use. I've now written a script that makes use of this module. The script was developed in VSCode using PowerShell 7 but it will run on other servers that may only have PowerShell 5.1 installed.

The requires statement of my script are as follows:

#Requires -Modules C:\MyFolder\MyModule.psm1

In PowerShell 7 (in Code and pwsh.exe window) it runs as expected.

In Powershell 5.1 I get the following error:

The script 'MyScript.ps1' cannot be run because the following modules that are
specified by the "#requires" statements of the script are missing: C:\MyFolder\MyModule.psm1.
At line:1 char:1

Does the requries statement not work with PS5? Any workaround to this other than installing PS7 or using Import-Module instead?

0

There are 0 answers