Adam Luniewski Adam Luniewski - 5 months ago 21
PowerShell Question

#Requires doesn't see the module, Import-Module works

When I run a simple script with

#Requires -Version 4 -Modules TestModule
statement at the first line, I get an error:

The script 'TestScript.ps1' cannot be run because the following
modules that are specified by the "#requires" statements of the script
are missing: TestModule
CategoryInfo : ResourceUnavailable: (TestScript.ps1:String) [], ScriptRequiresException FullyQualifiedErrorId :

Some interesting facts:

  1. The second time I run this script (same session), the module is loaded and everything works.

  2. Script directory is on a network share, but 7 other modules load without any problems (module types are Manifest, Script and Binary - they all worlk).

  3. One of the modules also has
    #Requires -Version 4 -Modules TestModule
    statement, but when I run a script with
    pointing to that module, both modules are loaded without any problems.

  4. Path is obviously added to

  5. Prior to running the script,
    Get-Module -ListAvailable
    does list TestModule and all its' commands.

  6. Import-Module
    works the first time.

  7. There may be something missing on the machine: anyone who logs on that computer is experiencing the same problem.


Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0
CLRVersion 4.0.30319.18444
BuildVersion 6.3.9600.16406

I have no idea what's going on.


Found the reason. I replaced #Requires with Import-Module TestModule -Verbose and found out that one of the assemblies could not be loaded, generating an error, but due to $ErrorActionPreference=Continue the rest of the module loaded. It appears that #Requires behaves differently than Import-Module.