powershelldenoexecutionpolicy

Module cannot be loaded on non-ps1 script


This command extracts the archive when running it directly in PowerShell 7.5.0-rc.1:

Expand-Archive -path .\test.zip -DestinationPath .

But running it via a Deno script:

new Deno.Command("PowerShell", {
  args: ["Expand-Archive -path ./test.zip -DestinationPath ."],
}).spawn();

yields this error:

Expand-Archive : The 'Expand-Archive' 
command was found in the module 
'Microsoft.PowerShell.Archive', but the 
module could not be loaded. For more 
information, run 'Import-Module 
Microsoft.PowerShell.Archive'.
At line:1 char:1
+ Expand-Archive -path ./test.zip 
-DestinationPath .
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound 
   : (Expand-Archive:String) [], CommandNot  
  FoundException
    + FullyQualifiedErrorId : CouldNotAutolo 
   adMatchingModule

I then run this script:

new Deno.Command("PowerShell", {
  args: ["Import-Module Microsoft.PowerShell.Archive"],
}).spawn();

new Deno.Command("PowerShell", {
  args: ["Expand-Archive -path ./test.zip -DestinationPath ."],
}).spawn();

and it yields this error:

Import-Module : File C:\program files\powersh
ell\7-preview\Modules\Microsoft.PowerShell.Ar
chive\Microsoft.PowerShell.Archive.psm1 
cannot be loaded because running scripts is 
disabled on this system. For more 
information, see about_Execution_Policies at 
https:/go.microsoft.com/fwlink/?LinkID=135170
.
At line:1 char:1
+ Import-Module Microsoft.PowerShell.Archive
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: 
    (:) [Import-Module], PSSecurityExceptio  
  n
    + FullyQualifiedErrorId : UnauthorizedAc 
   cess,Microsoft.PowerShell.Commands.Impor  
  tModuleCommand
Expand-Archive : The 'Expand-Archive' 
command was found in the module 
'Microsoft.PowerShell.Archive', but the 
module could not be loaded. For more 
information, run 'Import-Module 
Microsoft.PowerShell.Archive'.
At line:1 char:1
+ Expand-Archive -path ./test.zip 
-DestinationPath .
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound 
   : (Expand-Archive:String) [], CommandNot  
  FoundException
    + FullyQualifiedErrorId : CouldNotAutolo 
   adMatchingModule

Here is the current config of the execution policies:

Get-ExecutionPolicy -List

        Scope ExecutionPolicy
        ----- ---------------
MachinePolicy       Undefined
   UserPolicy       Undefined
      Process    Unrestricted
  CurrentUser       Undefined
 LocalMachine    Unrestricted

Do you have any idea?


Solution

  • when running it directly in PowerShell 7.5.0-rc.1:

    So it worked in PowerShell (Core) 7, but what you're calling from Deno is the CLI of Windows PowerShell, powershell.exe.

    These two editions by default have separate script-execution policies, and it is Windows PowerShell's effective execution policy that prevents loading of the module in your case.


    Thus, in order to use PowerShell 7's CLI, you must target pwsh.exe instead.

    That said, for predictable runtime behavior you should specify an execution policy ad hoc (only for the process being created), using the -ExecutionPolicy CLI parameter, which works with both powershell.exe and pwsh.exe:


    Applied to your case, using pwsh.exe (which is assumed to be discoverable via $env:PATH; use a full path, if needed):

    new Deno.Command("pwsh.exe", {
      args: ["-NoProfile", "-ExecutionPolicy", "RemoteSigned", "-Command", "Expand-Archive -path ./test.zip -DestinationPath ."],
    }).spawn();
    

    Caveat: