❔ Enable native/direct execution of .ps1 scripts - Warnings / Considerations
Intro: Recently, I have been doing a lot more scripting, both in PowerShell (.ps1) and Console/Shell (.cmd/.bat). I recognised that PowerShell is arguably a much more powerful / versatile scripting language, however I always found the ability to directly run a .cmd/.bat file (i.e.
C:> MyCmd) quite appealing (compared the relatively cumbersome
C:> powershell.exe -NoLogo -NoProfile -File "C:\Specify_the_full_path_to_my_powershell_folder\MyScript.ps1".
Today I happened to notice the
%PATHEXT% system variable and thought to myself: "Could it be that simple!?" (just add .ps1 to the list of extensions?) - It was! (for more info on how to do this, see the Linked question below, or click how to enable direct/native running of .ps1 files).
The relative ease of enabling this made me pause and wonder however...
- Microsoft make Windows
- Microsoft make PowerShell (and are actively driving the development of pwsh across multiple platforms)
- Microsoft have enabled 10+ extensions to be run directly: .com .exe .bat .cmd .vbs .vbe .js .jse .wsf .wsh .msc (several of which I have never even heard of!)
(Main question) Which forces me to ask...
- Why did Microsoft exclude .ps1 files from PATHEXT (i.e. stop them being run directly)!? (This seems like an obvious thing Microsoft would want to do)
- Are there any reasons why people should not do this?
- Any other downsides or disadvantages of doing this?
I feel like there is some obvious/major downside I must be over-looking, but can't think what that is. Yes, you could argue it is more risky / it's safer to load a PowerShell script into say PowerShell ISE and run it from there, but you could make that same statement for a .bat file - so would suggest there are some other reason(s) why running .ps1 scripts is not recommended.
Any advice would be greatly appreciated, thanks in advance! 👍🏼
🔁 Linked question: Enable direct/native running of .ps1 files — How To
Click here for a step-by-step guide on how to enable direct running of .ps1 files (assuming the answers do this question don't show that this is absolutely something you should never do)