Skip to content

Instantly share code, notes, and snippets.

@jamesmanning
Last active November 21, 2015 21:44
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 jamesmanning/b27aaac4910c170ab104 to your computer and use it in GitHub Desktop.
Save jamesmanning/b27aaac4910c170ab104 to your computer and use it in GitHub Desktop.
Create the registry keys necessary to get 'Open PowerShell here' in Windows Explorer context menus
Set-StrictMode -Version Latest
# since this won't work without running with admin privs, check for those first
$wid=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$prp=new-object System.Security.Principal.WindowsPrincipal($wid)
$adm=[System.Security.Principal.WindowsBuiltInRole]::Administrator
$IsAdmin=$prp.IsInRole($adm)
if ($IsAdmin -eq $false)
{
throw 'this must be run from an elevated PowerShell instance'
}
$commandToSet = (gcm powershell).path + " -NoExit -Command Set-Location -LiteralPath '%V'"
$keyLocationsToSet = (
'HKEY_CLASSES_ROOT\Directory\Background\shell\powershell', # this puts it in the context menu for Windows Explorer background
'HKEY_CLASSES_ROOT\Directory\shell\powershell', # this puts it in the context menu for Windows Explorer folders
'HKEY_CLASSES_ROOT\Drive\shell\powershell' # this puts it in the context menu for Windows Explorer drive icons
)
foreach ($key in $keyLocationsToSet)
{
write-host "Setting command to run of $commandToSet in location $key"
reg.exe add $key /f /ve /d "Open PowerShell Window Here"
reg.exe add $key /f /v NoWorkingDirectory
reg.exe add "$key\command" /f /ve /d $commandToSet
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment