Adding Personal PowerShell Commands to the Context Menu

by Apr 5, 2019

You can add personal context menu commands for file types like PowerShell files. These context menu commands appear when you right-click a .ps1 file. They are specific to your account, and you don’t need admin privileges to set them up.

Here is a script that does it for you. Just adjust the first two variables: specify the command as it should appear in the context menu, and the command to execute. In the command, use “%1” as a placeholder for the path to the PowerShell script path that was right-clicked:

# specify your command name
$ContextCommand = "Open Script with Notepad"
# specify the command to execute. "%1" represents the file path to your
# PowerShell script
$command = 'notepad "%1"'


$baseKey = 'Registry::HKEY_CLASSES_ROOT\.ps1'
$id = (Get-ItemProperty $baseKey).'(Default)'
$ownId = $ContextCommand.Replace(' ','')
$contextKey = "HKCU:\Software\Classes\$id\Shell\$ownId"
$commandKey = "$ContextKey\Command"

New-Item -Path $commandKey -Value $command -Force
Set-Item -Path $contextKey -Value $ContextCommand

Once you run this script, there is a new context menu command called “Open Script with Notepad”. You can hook up and design any command, including GitHub or backup commands for your scripts.

Important: Your custom commands will not show in the context menu when you chose a non-default application for OpenWith. The commands only appear when Notepad is your default OpenWith application.

To remove all custom context menu extensions, run this:

$baseKey = 'Registry::HKEY_CLASSES_ROOT\.ps1'
$id = (Get-ItemProperty $baseKey).'(Default)'
$contextKey = "HKCU:\Software\Classes\$id"
Remove-Item -Path $contextKey -Recurse -Force

psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!