-1

✅ Enable native/direct execution of .ps1 scripts - How To

In other words: treating .ps1 files the same as .cmd or .bat files - whereby you can double-click or type their name from the console/shell and it will run the script

Answer below: Through a bit of experimentation I was able to answer this question myself, so wanted to share for anyone who wanted to do this.


🔁 Linked question: Native/direct execution of .ps1 scripts - Warnings/Considerations

As part of my exploration of how to enable this, it raised the question of why this wasn't enabled by default (i.e. and so any warnings/reasons why you shouldn't do this). In an effort to best follow superuser.com guidelines, I have created a separate question to cover this.

0

Summary

There are two parts to enabling this (but both relatively easy)...
1️⃣ Adding ".PS1" to the PATHEXT environment variable
2️⃣ Specifying the host program and any behaviour options in the Registry


Part 1: PATHEXT

  1. Edit the System Environment variables - Run (elevated) rundll32.exe sysdm.cpl,EditEnvironmentVariables, or just search for "environment" / "variables" in the start menu
  2. In the second box, you will see a variable called PATHEXT, modify it
  3. Add ;.PS1 to the end of the list (separator + .ps1 extension).
    └ NOTE: The order or the extensions is important as it is the order Windows will search / execute files (e.g. if you have MyScript.bat, MyScript.cmd, MyScript.ps1 in the same directory and just type MyScript into the command shell, Windows will run the file matching the first extension it finds, searching in the order specified in PATHEXT

✅ Windows will now run a .ps1 script and (if you want) you can call a script without specifying the extension.
⚠️ However: Windows will use the default action/handler specified for the .ps1 filetype (in my case, this was actually opening the script in powershell_ise.exe), so you will likely want to adjust these as well (see Part 2)


Part 2: Default command (Registry)

(Before I made the above change I was already wondering: "Will use pwsh.exe or powershell.exe?" and "Will it use a profile, if so which one?" but found all of this is neatly handled by the default command and whatever you specify there is applied to a script when you run it)

The registry code is supplied below, but first here is an explanation of the components...

  1. You can choose to run the script with powershell.exe (version 5, included in Windows) or pwsh.exe (version 7, a more modern, cross-platform version you can install manually)
  2. By default, before running your PowerShell script, it will display the PowerShell header (copyright info, etc). Alternatively this can be disabled with -NoLogo (personally I prefer this, especially as it "blends" PowerShell more directly into the cmd shell, but accept that other users may prefer to keep cmd/PowerShell more distinct)
  3. If you have created a profile, PowerShell will load this before running your script. Alternatively this can be disabled with -NoProfile

Putting the above parts together

PowerShell Script (ps1) - Default action, run.reg
-------------------------------------------------

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.ps1\Shell]
@="pwsh_NoProfile"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.ps1\Shell\pwsh_NoProfile]
@="&Run in pwsh.exe"
"Icon"="C:\\Program Files\\PowerShell\\7\\pwsh.exe,0"

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\SystemFileAssociations\.ps1\Shell\pwsh_NoProfile\command]
@="\"C:\\Program Files\\PowerShell\\7\\pwsh.exe\" -NoLogo -NoProfile -File \"%1\""

NOTE: Normally, you would add commands to the filetype in the HKEY_CLASSES_ROOT, however Windows seem to have done something slightly odd to the .ps1 association, so in the above .reg file I have added the command(s) directly to the extension in the SFA section as this seem to work more reliably.


That's it!

The above environment variable change and registry update will let you run any .ps1 file by double-clicking it, or calling it's name in the console/cmd shell 👍🏼

(Finally, no more pwsh.exe -NoLogo -NoProfile -File "C:\path_to_my_powershell_scripts\MyPSScript.ps1", I can add "C:\path_to_my_powershell_scripts" to the %PATH% and just type MyPSScript - huzzah!! 🎉🎉)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.