Global Hotkeys
Global hotkeys for invoking PowerShell with keystrokes.
Global Hotkeys are only supported on Windows.
Global hotkeys allow you to assign hotkeys to PowerShell script blocks. You have access to the current foreground window and process so you can customize your hotkey actions based on what window is open.

Defining a Hotkey

Hotkeys are associated with the current PowerShell window. If you close the window, the hotkeys won't work. You can use the hotkeys anywhere on your desktop as long as PowerShell is open.
You can define hot keys using Set-Hotkey. This cmdlet allows you to specify the action to execute and the modifiers and key to invoke the action.
In the example, pressing Ctrl+B will open notepad.
1
Set-Hotkey -Action { Start-Process Notepad } -ModifierKeys Ctrl -Key B
Copied!

Passing Variables

You can pass variables by using the $ArgumentList parameter. These variables will appear in $args variable after the built in foreground and process ID variables.
1
$Process = "Notepad"
2
Set-Hotkey -Action { Start-Process $args[2] } -ArgumentList $Process -ModifierKeys Ctrl -Key B
Copied!

Accessing Foreground Window and Process Information

You can access the foreground window title and process information by using the $args variable. The first argument it the foreground window title and the second argument is the process ID.
This example will write the foreground window title to the console.
1
Set-Hotkey -Action { Write-Host $args[0] } -ModifierKeys Ctrl -Key B
Copied!
This example will open another instance of the selected process. The second argument is a process ID.
1
Set-Hotkey -Action { Start-Process (Get-Process -Id $args[1]).Path } -ModifierKeys Ctrl -Key B
Copied!

Retrieving Hotkeys

You can use Get-Hotkey to retrieve hotkeys that have been defined.
1
PS C:\Users\adamr> Get-Hotkey
2
3
Action ModifierKeys Keys
4
------ ------------ ----
5
Start-Process $args[1].Path Ctrl B
Copied!

Removing Hotkeys

You can remove hotkeys using Remove-Hotkey.
1
Get-Hotkey | Remove-Hotkey
Copied!
Last modified 10mo ago