Skip to content

Instantly share code, notes, and snippets.

@rodmhgl
Created January 9, 2017 16:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodmhgl/4a6b561e333caaea83fb389c1b4625a5 to your computer and use it in GitHub Desktop.
Save rodmhgl/4a6b561e333caaea83fb389c1b4625a5 to your computer and use it in GitHub Desktop.
Create PowerShell Here Right-Click Explorer Option
<#
.SYNOPSIS
Creates a right-click (or shift right-click) explorer menu option to open Powershell at the selected location
.DESCRIPTION
Creates a right-click (or shift right-click) explorer menu option to open Powershell at the selected location
.EXAMPLE
.\Create-PowerShellHere.ps1
Will create a right-click menu with the default text, "Open PowerShell Here"
.EXAMPLE
.\Create-PowerShellHere.ps1 -Extended
Will create a shift right-click menu with the default text, "Open PowerShell Here"
.EXAMPLE
.\Create-PowerShellHere.ps1 -Extended -MenuText "PoSH Prompt Here"
Will create a right-click menu with the specified text, "PoSH Prompt Here"
.NOTES
Credit to HowToGeek for the idea - http://www.howtogeek.com/165268/how-to-add-open-powershell-here-to-the-context-menu-in-windows
#>
param(
[switch]$Extended,
[string]$MenuText = "Open PowerShell Here"
)
$PowerShellMenuPath = "HKCR:\Directory\shell\powershellmenu"
$CommandPath = "HKCR:\Directory\shell\powershellmenu\command"
$PowerShellCL = "C:\\Windows\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -NoExit -Command Set-Location -LiteralPath '%L'"
try {
New-PSDrive -Name HKCR -PSProvider Registry -Root HKEY_CLASSES_ROOT -ErrorAction Stop
}
catch {
Write-Error "Unable to map HKEY_CLASSES_ROOT - Aborting"
Exit
}
finally {
Remove-PSDrive -Name HKCR -ErrorAction SilentlyContinue
}
New-Item -Path $PowerShellMenuPath -Force | Out-Null
New-Item -Path $CommandPath -Force | Out-Null
Set-ItemProperty -Path $PowerShellMenuPath -Name "(Default)" -Value $MenuText
Set-ItemProperty -Path $CommandPath -Name "(Default)" -Value $PowerShellCL
if ($Extended) {
Set-ItemProperty -Path $PowerShellMenuPath -Name "Extended" -Value $null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment