Skip to content

Instantly share code, notes, and snippets.

@E-K
Last active August 1, 2020 22:07
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 E-K/a07945fa24381b80aede9792681ff502 to your computer and use it in GitHub Desktop.
Save E-K/a07945fa24381b80aede9792681ff502 to your computer and use it in GitHub Desktop.
WindowsTerminalのsettings.jsonを読み込んでOpenHereメニューを作るpowershellスクリプト
#Requires -Version 7.0
Set-StrictMode -Version Latest
function CreateSubCommand {
param (
$prof,
[bool]$admin
)
$regKeyName = $prof.guid
if($admin) { $regKeyName = "{0}(admin)" -F $prof.guid }
New-Item $regKeyName
cd $regKeyName
#open
if($admin) {
New-ItemProperty -Path . -Name "HasLUAShield" -Type String
}
if([bool]($prof.PSobject.Properties.name -match "icon")) {
$icon = $prof.icon
if($prof.icon.StartsWith("ms-appx:")) {
$icon = $prof.commandline
}
if($icon) {
New-ItemProperty . -Name "Icon" -Type ExpandString -Value $icon
}
}
$name = $prof.name
if($admin) { $name = "{0} (Administrator)" -F $prof.name }
New-ItemProperty . -Name "MUIVerb" -Type String -Value $name
#command
New-Item "command"
cd "command"
$command = $null
if($admin) {
$command = "powershell.exe -command start-process '{0}' -ArgumentList('-d \`"%V\`"', '-p \`"{1}\`"') -Verb runas" -F "%LOCALAPPDATA%\Microsoft\WindowsApps\wt.exe", $prof.name
} else {
$command = "`"%LOCALAPPDATA%\Microsoft\WindowsApps\wt.exe`" -d `"%V`" -p `"{0}`"" -F $prof.name
}
New-ItemProperty . -Name "(default)" -Type ExpandString -Value $command
cd "../../"
}
$wt = Get-Item -Path ([IO.Path]::Combine($env:LOCALAPPDATA, "Microsoft\WindowsApps\wt.exe"))
$m = Select-String -InputObject $wt.Target -Pattern "WindowsApps\\(?<package>[a-zA-Z0-9_\.]+)\\WindowsTerminal\.exe"
$package = $m.Matches[0].Groups["package"].Value
$package
# Microsoft.WindowsTerminal_1.1.2021.0_x64__8wekyb3d8bbwe
$m = Select-String -InputObject $package -Pattern "Microsoft\.WindowsTerminal_(?<version>[0-9\.]+)_(?<arch>[0-9a-zA-Z]+)__(?<hash>[0-9a-zA-Z]+)"
# $version = $m.Matches[0].Groups["version"].Value
# $arch = $m.Matches[0].Groups["arch"].Value
$hash = $m.Matches[0].Groups["hash"].Value
#C:\Users\E-K\AppData\Local\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json
$jsonPath = [IO.Path]::Combine($env:LOCALAPPDATA, "Packages", "Microsoft.WindowsTerminal_${hash}", "LocalState\settings.json")
$json = Get-Content -Path $jsonPath -Raw | ConvertFrom-Json
Push-Location "HKCU:\SOFTWARE\Classes"
$regCommandName = "OpenWindowsTerminalEx"
# HKCU:\SOFTWARE\Classes\Directory\Background\shell\${regCommandName}
if(-not (Test-Path "Directory")) {
New-Item "Directory"
}
cd "Directory"
if(-not (Test-Path "Background")) {
New-Item "Background"
}
cd "Background"
if(-not (Test-Path "shell")) {
New-Item "shell"
}
cd "shell"
if(Test-Path $regCommandName) {
Remove-Item $regCommandName
}
New-Item $regCommandName
New-ItemProperty -Path $regCommandName -Name "ExtendedSubCommandsKey" -Type String -Value "Directory\ContextMenus\${regCommandName}"
New-ItemProperty -Path $regCommandName -Name "Icon" -Type String -Value $wt.Target
New-ItemProperty -Path $regCommandName -Name "MUIVerb" -Type String -Value "Windows Terminal"
Pop-Location
Push-Location "HKCU:\SOFTWARE\Classes\Directory"
if(-not (Test-Path "ContextMenus")) {
New-Item "ContextMenus"
}
cd "ContextMenus"
if(Test-Path $regCommandName) {
Remove-Item $regCommandName -Recurse
}
New-Item $regCommandName
cd $regCommandName
New-Item "shell"
cd "shell"
foreach ($prof in $json.profiles) {
CreateSubCommand -prof $prof -admin $false
CreateSubCommand -prof $prof -admin $true
}
Pop-Location
@E-K
Copy link
Author

E-K commented Aug 1, 2020

powershell 7 でのみ動作確認をしています

@E-K
Copy link
Author

E-K commented Aug 1, 2020

windows 1909で管理者実行が動作しない

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment