Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@espoelstra
Created September 24, 2018 02:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save espoelstra/913b6bff982f9b45547ad10686f9e43e to your computer and use it in GitHub Desktop.
Save espoelstra/913b6bff982f9b45547ad10686f9e43e to your computer and use it in GitHub Desktop.
PowerShell hijinks

If wanting to run a .ps1 with a double click it IS possible to edit the registry to allow this, the issue is that scripts that aren't elevated may fail, and it bypasses some of the ExecutionPolicy restrictions or hardcodes the same policy for all of them.

This Q&A has a number of options for making it work, but the best practice scenario I'm going with is creating a .bat file to call a .ps1 file, and then putting a shortcut to the .bat file on the Public Desktop so that the shortcut can be set to run as Administrator and prompt for the password if UAC is configured that way.

https://stackoverflow.com/questions/10137146/is-there-any-way-to-make-powershell-script-work-by-double-clicking-ps1-file

For reference the "Run with PowerShell" in the right click menu uses this as the "Command" registry key, "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" "-Command" "if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'"

If a user has changed the association via the GUI there is an override in their registry path. You can simply delete this to use the system-wide setting. HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1\UserChoice HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1\OpenWithList Making a choice to use another program also creates an OpenWithList folder/key that can be deleted to reset the Open With to just the default program as well.

There is an empty entry in HKEY_LOCAL_MACHINE\SOFTWARE\Classes\.ps1

Other keys pertaining to Microsoft.PowerShellScript.1 HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Microsoft.PowerShellScript.1 HKEY_USERS\S-1-5-21-2600432384-240279299-2107569243-3351\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1 HKEY_USERS\S-1-5-21-2600432384-240279299-2107569243-3351\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1 HKEY_CLASSES_ROOT\.ps1 HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts\.ps1

@echo off
pushd "%~d0"
pushd "%~dp0"
REM Using %~dp0 to pass the full path of the .ps1 file, when triggered from the .bat it uses the basename of the .bat file so make sure the .bat and .ps1 names match.
REM Elevation doesn't pass from the .bat to the Powershell so you need the RunAs
Powershell.exe -Command "& {Start-Process PowerShell.exe -Verb RunAs -ArgumentList '-File %~dp0\%~n0.ps1 %*'}"
popd
popd
REM pause just keeps the cmd prompt open, all the action is in the Powershell window and with @echo off the command prompt is essentially empty anyways
pause
Write-Host "You wanted something?"
Read-Host -Prompt “Press Enter to exit”
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment