After adjusting the PATHEXT and PATH environment variables, PowerShell scripts can accessed from the Command Prompt. The issue comes, however, is that when attempting to execute a script (ie, CMD> foobar where foobar.ps1 is on the path) Notepad opens the script instead of Powershell being invoked to execute the script.
The goal is to be able to run CMD> foobar and invoke the PowerShell script and return the results directly into the console.
Here are a few solutions to the problem that somewhat come close to, but don't, meet the needs:
- Setting the Execution Policy to a more permissive setting
- While this helps, it doesn't let me run the
.ps1s directly from the CMD command line.
- While this helps, it doesn't let me run the
- Opening the PowerShell prompt and running the script
- This kind of defeats the purpose. While I am shifting to a PowerShell-first mindset, I need solutions in the interim.
- Setting registry keys to double click the script
- This helps somewhat, but I don't think it actually does anything for this problem.
- Prefixing the command with
powershell -File- This doesn't solve the problem because I want to
CMD> foobar, notCMD> powershell -File foobar.
- This doesn't solve the problem because I want to
PowerShell.exe -ExecutionPolicy Bypass -File .foobar.ps1meant to be any better? How is this not an issue with .bat scripts - I can run them directly from the command prompt? – TheBrenny Dec 23 '22 at 04:19PowerShell.exe -ExecutionPolicy Bypass -File .foobar.ps1is not valid. That switch is only valid when you call powershell/pwsh.exe. So, changing the association, without manually setting the EP, means the file would not run anyway if that EP is set to say restricted by your org. So, to do what you say, that EP must also be set properly in advance. – postanote Dec 23 '22 at 05:23