Skip to content

Instantly share code, notes, and snippets.

@fredrikhr
Last active February 19, 2024 17:47
Show Gist options
  • Star 18 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fredrikhr/0994a6e81443c439659db1d9763365d7 to your computer and use it in GitHub Desktop.
Save fredrikhr/0994a6e81443c439659db1d9763365d7 to your computer and use it in GitHub Desktop.

Create Windows Terminal shortcut in Windows + X context menu

Windows Terminal shortcut in WinX context menu

In Windows PowerShell do the following:

$folderPath = "$ENV:LOCALAPPDATA\Microsoft\Windows\WinX\Group3"
$adminFilePath = Join-Path $folderPath "00 - Windows Terminal.lnk"
$normalFilePath = Join-Path $folderPath "00a - Windows Terminal.lnk"
$adminDisplay = "Windows Terminal (&Admin)"
$normalDisplay = "Windows &Terminal"

$shortcutFile = Join-Path $ENV:TEMP "Windows Terminal Shortcut.lnk"
Invoke-WebRequest -OutFile $shortcutFile -Uri "https://gist.github.com/fredrikhr/0994a6e81443c439659db1d9763365d7/raw/Windows%2520Terminal%2520Shortcut.lnk"
Copy-Item $shortcutFile $adminFilePath -Force -Verbose
Copy-Item $shortcutFile $normalFilePath -Force -Verbose
Remove-Item -Verbose -Force $shortcutFile

# Get the Windows Shell COM service instance:
$shell = New-Object -COM WScript.Shell

# Change the Admin Shortcut display name (Description) and mark as Run as Administrator
$adminShortcut = $shell.CreateShortcut($adminFilePath)
$adminShortcut.Description = $adminDisplay # Set Display in Win+X menu using A as the mnemonic character
$adminShortcut.Save()
# Setting the Run as administrator flag does not have an API, flip the bit manually
# ref.: https://stackoverflow.com/a/29002207/2226662
$adminBytes = [System.IO.File]::ReadAllBytes($adminFilePath)
$adminBytes[0x15] = $adminBytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON
[System.IO.File]::WriteAllBytes($adminFilePath, $adminBytes)

# Change the Normal Shortcut display name (Description)
$normalShortcut = $shell.CreateShortcut($normalFilePath)
$normalShortcut.Description = $normalDisplay # Set Display in Win+X menu using T as the mnemonic character
$normalShortcut.Save()

After restarting the explorer.exe shell, or by logging out and back in again, the Windows Terminal shortcuts should appear in the Windows+X context menu.

Shortcut Hashing

Shortcut files in the WinX folder have to be hashed correctly to be accepted by Windows. There is a tool hashlnk that can be used to hash shortcut links. I have applied the correct hash to the .lnk file supplied here.

> hashlnk.exe "Windows Terminal Shortcut.lnk"

HashLnk v0.2.0.0
Copyright(c) 2012 Rafael Rivera
Within Windows - http://withinwindows.com

Hash generated and applied (0x4B665D57)

Windows Terminal shortcut

If you inspect the Windows Terminal Shortcut supplied here, you will notice that it does not refer to the Windows Terminal app by a fixed path. Instead, since Windows Terminal is a Windows Store app, the shortcut refers to the ApplicationId of the Windows Terminal application.

This ensures that Windows will launch the app regardless of where you have it installed. This means that I can happily share my Windows Terminal shortcut file and it will work just fine on any Windows OS that has the Windows Terminal app installed.

Extensibility

You can change the first line in the PowerShell code snippet to modify the path to where the WinX menu files are located. The code snippet will currently use the path of the WinX for current user (using the LOCALAPPDATA environment variable)

Default User modification

To apply the Windows Terminal shortcut for all new users on a Windows machine, you can modify the AppData\Local folder of the Default user instead.

The Group3 folder for the Default user is usually located at: C:\Users\Default\AppData\Local\Microsoft\Windows\WinX\Group3

Therefore, exchange the first line in the code snippet above to:

$folderPath = "C:\Users\Default\AppData\Local\Microsoft\Windows\WinX\Group3"

Replace Command Prompt

The code snippet above uses &T and &A as the mnemonic character for the Windows Terminal shortcuts. Unfortunately, T and A mnemonics already exist in the WinX menu by default (if using the English (US) language).
T is used by the Task Manager shortcut in Group2 and A is used by Command Prompt (Admin) (or the PowerShell equivalent).

Since the goal usually is to replace the Command Prompt shortcut with Windows Terminal, you might want to do this instead:

Replace lines 2-4 as follows:

$adminFilePath = Join-Path $folderPath "01 - Command Prompt.lnk"
$normalFilePath = Join-Path $folderPath "02 - Command Prompt.lnk"
$adminDisplay = "Command Prompt (&Admin)"
$normalDisplay = "&Command Prompt"

NOTE: this will only work if the following setting in the Settings app is turned off: PersonalizationTaskbarReplace Command Prompt with Windows PowerShell in the menu when I right-click the start menu button or press Windows key+X

References & Acknowledgements

I was originally inspired by James Rankin who explains in detail how to replace the settings shortcut in the Win+X menu with the old Control Panel:
Customizing the WinX menu, and making it user-specific

Thanks to Rafael Rivera who wrote two blog articles and released the hashlnk tool to properly hash Shortcut files:

Also thanks to this Anwer on StackOverflow for Setting the Run as Administrator flag in a Shortcut.

$folderPath = "$ENV:LOCALAPPDATA\Microsoft\Windows\WinX\Group3"
$adminFilePath = Join-Path $folderPath "01 - Command Prompt.lnk"
$normalFilePath = Join-Path $folderPath "02 - Command Prompt.lnk"
$adminDisplay = "Command Prompt (&Admin)"
$normalDisplay = "&Command Prompt"
$shortcutFile = Join-Path $ENV:TEMP "Windows Terminal Shortcut.lnk"
Invoke-WebRequest -OutFile $shortcutFile -Uri "https://gist.github.com/fredrikhr/0994a6e81443c439659db1d9763365d7/raw/Windows%2520Terminal%2520Shortcut.lnk"
Copy-Item $shortcutFile $adminFilePath -Force -Verbose
Copy-Item $shortcutFile $normalFilePath -Force -Verbose
Remove-Item -Verbose -Force $shortcutFile
# Get the Windows Shell COM service instance:
$shell = New-Object -COM WScript.Shell
# Change the Admin Shortcut display name (Description) and mark as Run as Administrator
$adminShortcut = $shell.CreateShortcut($adminFilePath)
$adminShortcut.Description = $adminDisplay # Set Display in Win+X menu using A as the mnemonic character
$adminShortcut.Save()
# Setting the Run as administrator flag does not have an API, flip the bit manually
# ref.: https://stackoverflow.com/a/29002207/2226662
$adminBytes = [System.IO.File]::ReadAllBytes($adminFilePath)
$adminBytes[0x15] = $adminBytes[0x15] -bor 0x20 #set byte 21 (0x15) bit 6 (0x20) ON
[System.IO.File]::WriteAllBytes($adminFilePath, $adminBytes)
# Change the Normal Shortcut display name (Description)
$normalShortcut = $shell.CreateShortcut($normalFilePath)
$normalShortcut.Description = $normalDisplay # Set Display in Win+X menu using T as the mnemonic character
$normalShortcut.Save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment