Here's the answer, at a Windows command-line:
powershell .\ps4.cmd 'PowerShell.exe -ExecutionPolicy Bypass -File .\zipper.ps1'
Explanation:
My solution is based on putting the script into a script file, and executing. In code below, ps4.cmd
is the PowerShell script mentioned in the OP. Commands below are executed at a powershell prompt:
.\ps4.cmd .\zipper.ps1
.\
ensures the powershell will look for the scripts in the active directory.
or,
.\ps4 'Get-Content .\zipper.ps1 | Invoke-Expression'
equivalent to:
.\ps4 'GC .\zipper.ps1 | iex'
Execution Policy
By default, Windows ExecutionPolicy is "restricted", which will prevent any scripts from running. Changing ExecutionPolicy system-wide requires Admin access. I'm including workarounds for this since, in my use-case, it's a critical blocker-- even if you change the security policy for powershell, that might not apply to the ps4.cmd environment.
Here are a couple ways to bypass the security restriction without Admin access:
PowerShell.exe -ExecutionPolicy Bypass -File .\zipper.ps1
So, how can we pass the script to ps4.cmd and bypass security?
.\ps4.cmd 'PowerShell.exe -ExecutionPolicy Bypass -File .\zipper.ps1'
the single-quotes are not required, unless there are spaces in your script names or paths.
To run from the Windows command-line (wish i could make this terser, but it's the answer):
powershell .\ps4.cmd 'PowerShell.exe -ExecutionPolicy Bypass -File .\zipper.ps1'
or,
.\ps4 'GC .\zipper.ps1 | iex'
Yep, same as #2 above. We pass a script to the external script and bypass security-restriction, in one simple statement. For its terseness, this would be my preferred answer, but my concern about the GC method is-- are we really executing individual statements? Or just one big string? If it's just one big string, then this does not satisfy the OP.
Note, plz do not flag this answer as a "security exploit"--
Microsoft never intended [execution policy] to be a security control. Which is why there are so many options for bypassing it. Microsoft was nice enough to provide some native options https://diigo.com/08xzeu