Skip to content

Instantly share code, notes, and snippets.

@SCP002
Last active October 4, 2022 22:33
Show Gist options
  • Save SCP002/a1700041a969b4eb6af58248c6853222 to your computer and use it in GitHub Desktop.
Save SCP002/a1700041a969b4eb6af58248c6853222 to your computer and use it in GitHub Desktop.
Windows Registry: Run PowerShell scripts with double click. Various tweaks of context menu for .ps1 and .psm1 files. See coments for description.
Windows Registry Editor Version 5.00
; WARNING:
; Uses Visual Studio Code as a standard editor.
; You might change it to your own with default file path, like:
;
; [HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Edit\Command]
; @="\"C:\\Windows\\System32\\notepad.exe\" \"%1\""
;
; Or using path variables (%SystemRoot% etc.) for portability sake by
; creating REG_EXPAND_SZ default value:
; @=hex(2):...
; with binary representation of a value like:
; "%SystemRoot%\System32\notepad.exe" "%1"
; Tested with Windows 10 Pro 20H2, build 19042.928
; ========================================================= SCRIPTS =========================================================
; Associate .ps1 files with powershell scripts
; --------------------------------------------------------------------
; Set default value to 'Microsoft.PowerShellScript.1'
[HKEY_CLASSES_ROOT\.ps1]
@="Microsoft.PowerShellScript.1"
; Fix open .ps1 files with any program except 'powershell.exe' or 'powershell_ise.exe' cause content menu settings breakage
; --------------------------------------------------------------------
; Remove 'OpenWithProgids'
[-HKEY_CLASSES_ROOT\.ps1\OpenWithProgids]
; Change powershell scripts context menu behavior: Add, remove and change default items
; --------------------------------------------------------------------
; Set default action to 'Open'
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell]
@="Open"
; Remove 'Run with PowerShell'
[-HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0]
; Set 'Edit' action to open script with VSCode
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Edit]
"NoSmartScreen"=""
; REG_EXPAND_SZ value of: "%LocalAppData%\Programs\Microsoft VS Code\Code.exe" "%1"
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Edit\Command]
@=hex(2):22,00,25,00,4c,00,6f,00,63,00,61,00,6c,00,41,00,70,00,70,00,44,00,61,\
00,74,00,61,00,25,00,5c,00,50,00,72,00,6f,00,67,00,72,00,61,00,6d,00,73,00,\
5c,00,4d,00,69,00,63,00,72,00,6f,00,73,00,6f,00,66,00,74,00,20,00,56,00,53,\
00,20,00,43,00,6f,00,64,00,65,00,5c,00,43,00,6f,00,64,00,65,00,2e,00,65,00,\
78,00,65,00,22,00,20,00,22,00,25,00,31,00,22,00,00,00
; Set 'Open' action to run script
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open]
; REG_EXPAND_SZ value of: "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser -Force; & \"%1\""
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\Open\Command]
@=hex(2):22,00,25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,\
00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,57,00,\
69,00,6e,00,64,00,6f,00,77,00,73,00,50,00,6f,00,77,00,65,00,72,00,53,00,68,\
00,65,00,6c,00,6c,00,5c,00,76,00,31,00,2e,00,30,00,5c,00,70,00,6f,00,77,00,\
65,00,72,00,73,00,68,00,65,00,6c,00,6c,00,2e,00,65,00,78,00,65,00,22,00,20,\
00,22,00,2d,00,43,00,6f,00,6d,00,6d,00,61,00,6e,00,64,00,22,00,20,00,22,00,\
53,00,65,00,74,00,2d,00,45,00,78,00,65,00,63,00,75,00,74,00,69,00,6f,00,6e,\
00,50,00,6f,00,6c,00,69,00,63,00,79,00,20,00,2d,00,45,00,78,00,65,00,63,00,\
75,00,74,00,69,00,6f,00,6e,00,50,00,6f,00,6c,00,69,00,63,00,79,00,20,00,42,\
00,79,00,70,00,61,00,73,00,73,00,20,00,2d,00,53,00,63,00,6f,00,70,00,65,00,\
20,00,43,00,75,00,72,00,72,00,65,00,6e,00,74,00,55,00,73,00,65,00,72,00,20,\
00,2d,00,46,00,6f,00,72,00,63,00,65,00,3b,00,20,00,26,00,20,00,5c,00,22,00,\
25,00,31,00,5c,00,22,00,22,00,00,00
; Add 'Run as administrator'
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\RunAs]
"HasLUAShield"=""
; REG_EXPAND_SZ value of: "%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope CurrentUser -Force; & \"%1\""
[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\RunAs\Command]
@=hex(2):22,00,25,00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,\
00,25,00,5c,00,53,00,79,00,73,00,74,00,65,00,6d,00,33,00,32,00,5c,00,57,00,\
69,00,6e,00,64,00,6f,00,77,00,73,00,50,00,6f,00,77,00,65,00,72,00,53,00,68,\
00,65,00,6c,00,6c,00,5c,00,76,00,31,00,2e,00,30,00,5c,00,70,00,6f,00,77,00,\
65,00,72,00,73,00,68,00,65,00,6c,00,6c,00,2e,00,65,00,78,00,65,00,22,00,20,\
00,22,00,2d,00,43,00,6f,00,6d,00,6d,00,61,00,6e,00,64,00,22,00,20,00,22,00,\
53,00,65,00,74,00,2d,00,45,00,78,00,65,00,63,00,75,00,74,00,69,00,6f,00,6e,\
00,50,00,6f,00,6c,00,69,00,63,00,79,00,20,00,2d,00,45,00,78,00,65,00,63,00,\
75,00,74,00,69,00,6f,00,6e,00,50,00,6f,00,6c,00,69,00,63,00,79,00,20,00,42,\
00,79,00,70,00,61,00,73,00,73,00,20,00,2d,00,53,00,63,00,6f,00,70,00,65,00,\
20,00,43,00,75,00,72,00,72,00,65,00,6e,00,74,00,55,00,73,00,65,00,72,00,20,\
00,2d,00,46,00,6f,00,72,00,63,00,65,00,3b,00,20,00,26,00,20,00,5c,00,22,00,\
25,00,31,00,5c,00,22,00,22,00,00,00
; ========================================================= MODULES =========================================================
; Associate .psm1 files with powershell modules
; --------------------------------------------------------------------
; Set default value to 'Microsoft.PowerShellModule.1'
[HKEY_CLASSES_ROOT\.psm1]
@="Microsoft.PowerShellModule.1"
; Fix open .psm1 files with any program except 'powershell.exe' or 'powershell_ise.exe' cause content menu settings breakage
; --------------------------------------------------------------------
; Remove 'OpenWithProgids'
[-HKEY_CLASSES_ROOT\.psm1\OpenWithProgids]
; Change powershell modules context menu behavior: Add, remove and change default items
; --------------------------------------------------------------------
; Set default action to 'Open'
[HKEY_CLASSES_ROOT\Microsoft.PowerShellModule.1\Shell]
@="Open"
; Remove 'Run with PowerShell'
[-HKEY_CLASSES_ROOT\Microsoft.PowerShellModule.1\Shell\0]
; Set 'Edit' action to open module with VSCode
[HKEY_CLASSES_ROOT\Microsoft.PowerShellModule.1\Shell\Edit]
"NoSmartScreen"=""
; REG_EXPAND_SZ value of: "%LocalAppData%\Programs\Microsoft VS Code\Code.exe" "%1"
[HKEY_CLASSES_ROOT\Microsoft.PowerShellModule.1\Shell\Edit\Command]
@=hex(2):22,00,25,00,4c,00,6f,00,63,00,61,00,6c,00,41,00,70,00,70,00,44,00,61,\
00,74,00,61,00,25,00,5c,00,50,00,72,00,6f,00,67,00,72,00,61,00,6d,00,73,00,\
5c,00,4d,00,69,00,63,00,72,00,6f,00,73,00,6f,00,66,00,74,00,20,00,56,00,53,\
00,20,00,43,00,6f,00,64,00,65,00,5c,00,43,00,6f,00,64,00,65,00,2e,00,65,00,\
78,00,65,00,22,00,20,00,22,00,25,00,31,00,22,00,00,00
; Set 'Open' action to open module with VSCode
[HKEY_CLASSES_ROOT\Microsoft.PowerShellModule.1\Shell\Open]
"NoSmartScreen"=""
; REG_EXPAND_SZ value of: "%LocalAppData%\Programs\Microsoft VS Code\Code.exe" "%1"
[HKEY_CLASSES_ROOT\Microsoft.PowerShellModule.1\Shell\Open\Command]
@=hex(2):22,00,25,00,4c,00,6f,00,63,00,61,00,6c,00,41,00,70,00,70,00,44,00,61,\
00,74,00,61,00,25,00,5c,00,50,00,72,00,6f,00,67,00,72,00,61,00,6d,00,73,00,\
5c,00,4d,00,69,00,63,00,72,00,6f,00,73,00,6f,00,66,00,74,00,20,00,56,00,53,\
00,20,00,43,00,6f,00,64,00,65,00,5c,00,43,00,6f,00,64,00,65,00,2e,00,65,00,\
78,00,65,00,22,00,20,00,22,00,25,00,31,00,22,00,00,00
; Remove 'Run as administrator'
[-HKEY_CLASSES_ROOT\Microsoft.PowerShellModule.1\Shell\RunAs]
@AndrewS097
Copy link

Remove 'Run with PowerShell' and 'Run as administrator' does not work. Is it Windows version dependent? Is there any alternative that could be used?

@SCP002
Copy link
Author

SCP002 commented Oct 4, 2022

Remove 'Run with PowerShell' and 'Run as administrator' does not work. Is it Windows version dependent? Is there any alternative that could be used?

You are correct, just checked it and on Windows 10 Pro 21H2, build 19044.2006.
Menu 'Run with PowerShell' does not disappear anymore after you delete the HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\0 registry folder.

But it worked on Windows 10 Pro 20H2, build 19042.928 from what I remember.

No clue so far on how to get rid of 'Run with PowerShell' now 😞

And about 'Run as administrator': It's the option this .reg file tries to add, not remove.
This one works fine for me.
But if you want to remove 'Run as administrator' from the list of context menu options, remove the HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\RunAs folder in registry editor.
Or just add [-HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\RunAs] to this .reg file.

@AndrewS097
Copy link

Forsure I know you can modify the Run with PowerShell (name) if you modify the Muicache Computer\HKEY_CLASSES_ROOT\Local Settings\MuiCache, but there is no way to outright prevent it. Currently the .exe in MUICache is @"C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe ",-108. Please let me know if you figure out an alternative for removing it. ):

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment