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
Part 1: PATHEXT
- Edit the System Environment variables - Run (elevated)
rundll32.exe sysdm.cpl,EditEnvironmentVariables, or just search for "environment" / "variables" in the start menu
- In the second box, you will see a variable called
PATHEXT, modify it
;.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.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...
- 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)
- 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)
- If you have created a profile, PowerShell will load this before running your script. Alternatively this can be disabled with
Putting the above parts together
PowerShell Script (ps1) - Default action, run.reg
Windows Registry Editor Version 5.00
@="&Run in pwsh.exe"
@="\"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.
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!! 🎉🎉)