From 0362fbb86c2daf1c5a2128b76c2290ec65f8cb64 Mon Sep 17 00:00:00 2001 From: Peter Rhodes Date: Wed, 22 Sep 2021 11:21:25 +0100 Subject: [PATCH 01/10] First working install.ps1, requires manual username entry in script --- install.ps1 | 79 +++++++++++++++++++++++++++++++---------------------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/install.ps1 b/install.ps1 index 5d513ab..8f62f1c 100644 --- a/install.ps1 +++ b/install.ps1 @@ -10,6 +10,19 @@ param( [switch] $PreRelease ) +#Specify target user if different from account running the script, else comment this out +$targetUser = "example.username" # Optional - specify user. Otherwise, uncomment the below +# $targetUser = $Env:UserName + +$targetUserObject = New-Object System.Security.Principal.NTAccount($targetUser) +$targetUserSID = $targetUserObject.Translate([System.Security.Principal.SecurityIdentifier]).value + +echo "Changing settings for user $targetUser with SID $targetUserSID" + +$Env:LocalAppData = "C:\Users\$targetUser\AppData\Local" +$Env:LOCALAPPDATA = $Env:LocalAppData + + function Generate-HelperScript( # The cache folder [Parameter(Mandatory=$true)] @@ -366,15 +379,15 @@ function CreateProfileMenuItems( $profileIcon = GetProfileIcon $profile $folder $localCache $icon $isPreview if ($layout -eq "Default") { - $rootKey = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell\$guid" - $rootKeyElevated = "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell\$guid" + $rootKey = "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell\$guid" + $rootKeyElevated = "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell\$guid" CreateMenuItem $rootKey $name $profileIcon $command $false CreateMenuItem $rootKeyElevated $name $profileIcon $elevated $true } elseif ($layout -eq "Flat") { - CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false - CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true - CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false - CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true + CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false + CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true + CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false + CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true } } @@ -398,36 +411,36 @@ function CreateMenuItems( if ($layout -eq "Default") { # defaut layout creates two menus - New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Force | Out-Null - New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null - New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Name 'Icon' -PropertyType String -Value $icon | Out-Null - New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null + New-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Force | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'Icon' -PropertyType String -Value $icon | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null - New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Force | Out-Null - New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null - New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Name 'Icon' -PropertyType String -Value $icon | Out-Null - New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null + New-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Force | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'Icon' -PropertyType String -Value $icon | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null - New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell' -Force | Out-Null + New-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell" -Force | Out-Null - New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Force | Out-Null - New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null - New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Name 'Icon' -PropertyType String -Value $icon | Out-Null - New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null + New-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Force | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'Icon' -PropertyType String -Value $icon | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null - New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Force | Out-Null - New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null - New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Name 'Icon' -PropertyType String -Value $icon | Out-Null - New-ItemProperty -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null + New-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Force | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'Icon' -PropertyType String -Value $icon | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null - New-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell' -Force | Out-Null + New-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell" -Force | Out-Null } elseif ($layout -eq "Mini") { $command = """$executable"" -d ""%V.""" $elevated = "wscript.exe ""$localCache/helper.vbs"" ""$executable"" ""%V.""" - CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false - CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true - CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false - CreateMenuItem "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true + CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false + CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true + CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false + CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true return } @@ -445,11 +458,11 @@ if ((Get-Process -Id $pid).Path -like "*WindowsApps*") { exit 1 } -if ((Test-Path "Registry::HKEY_CLASSES_ROOT\Directory\shell\MenuTerminal") -and - -not (Test-Path "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal")) { - Write-Error "Please execute uninstall.old.ps1 to remove previous installation." - exit 1 -} +#if ((Test-Path "Registry::HKEY_CLASSES_ROOT\Directory\shell\MenuTerminal") -and +# -not (Test-Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal")) { +# Write-Error "Please execute uninstall.old.ps1 to remove previous installation." +# exit 1 +#} if ($PSVersionTable.PSVersion.Major -lt 6) { Write-Error "Must be executed in PowerShell 6 and above. Learn how to install it from https://docs.microsoft.com/en-us/powershell/scripting/install/installing-powershell-core-on-windows?view=powershell-7 . Exit." From b01c7fb3d9c334ed34e47bc2065633233b572231 Mon Sep 17 00:00:00 2001 From: Peter Rhodes Date: Wed, 22 Sep 2021 11:25:59 +0100 Subject: [PATCH 02/10] Untested equivalent mods to uninstall script --- uninstall.ps1 | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/uninstall.ps1 b/uninstall.ps1 index 1ca6c73..86a2e94 100644 --- a/uninstall.ps1 +++ b/uninstall.ps1 @@ -9,6 +9,16 @@ param( [switch] $PreRelease ) +#Specify target user if different from account running the script, else comment this out +$targetUser = "example.username" # Optional - specify user. Otherwise, uncomment the below +# $targetUser = $Env:UserName + +$targetUserObject = New-Object System.Security.Principal.NTAccount($targetUser) +$targetUserSID = $targetUserObject.Translate([System.Security.Principal.SecurityIdentifier]).value + +$Env:LocalAppData = "C:\Users\$targetUser\AppData\Local" +$Env:LOCALAPPDATA = $Env:LocalAppData + # Based on @nerdio01's version in https://github.com/microsoft/terminal/issues/1060 if ((Get-Process -Id $pid).Path -like "*WindowsApps*") { @@ -17,7 +27,7 @@ if ((Get-Process -Id $pid).Path -like "*WindowsApps*") { } if ((Test-Path "Registry::HKEY_CLASSES_ROOT\Directory\shell\MenuTerminal") -and - -not (Test-Path "Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal")) { + -not (Test-Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal")) { Write-Error "Please execute uninstall.old.ps1 to remove previous installation." exit 1 } @@ -30,12 +40,12 @@ if (Test-Path $localCache) { Write-Host "Use" $layout "layout." if ($layout -eq "Default") { - Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminal' -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal' -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell' -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin' -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin' -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell' -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell" -Recurse -ErrorAction Ignore | Out-Null } elseif ($layout -eq "Flat") { $rootKey = 'HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell' foreach ($key in Get-ChildItem -Path "Registry::$rootKey") { @@ -51,10 +61,10 @@ if ($layout -eq "Default") { } } } elseif ($layout -eq "Mini") { - Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalMini' -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell\MenuTerminalAdminMini' -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini' -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path 'Registry::HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdminMini' -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalMini" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdminMini" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdminMini" -Recurse -ErrorAction Ignore | Out-Null } Write-Host "Windows Terminal uninstalled from Windows Explorer context menu." From 47621a88f4c8c2b2a5a60726f02cbaad593697ee Mon Sep 17 00:00:00 2001 From: Peter Rhodes <51478811+peter1rhodes@users.noreply.github.com> Date: Wed, 22 Sep 2021 11:58:34 +0100 Subject: [PATCH 03/10] Default to current user Default to the current user to preserve standard behaviour --- install.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/install.ps1 b/install.ps1 index 8f62f1c..2714388 100644 --- a/install.ps1 +++ b/install.ps1 @@ -11,8 +11,8 @@ param( ) #Specify target user if different from account running the script, else comment this out -$targetUser = "example.username" # Optional - specify user. Otherwise, uncomment the below -# $targetUser = $Env:UserName +# $targetUser = "example.username" # Optional - specify user. Otherwise, uncomment the below +$targetUser = $Env:UserName $targetUserObject = New-Object System.Security.Principal.NTAccount($targetUser) $targetUserSID = $targetUserObject.Translate([System.Security.Principal.SecurityIdentifier]).value From be898fe585df11a1ba04452fce068855c05da500 Mon Sep 17 00:00:00 2001 From: Peter Rhodes <51478811+peter1rhodes@users.noreply.github.com> Date: Wed, 22 Sep 2021 11:59:04 +0100 Subject: [PATCH 04/10] Default to current user Default to the current user to preserve existing behaviour --- uninstall.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/uninstall.ps1 b/uninstall.ps1 index 86a2e94..bb6e985 100644 --- a/uninstall.ps1 +++ b/uninstall.ps1 @@ -10,8 +10,8 @@ param( ) #Specify target user if different from account running the script, else comment this out -$targetUser = "example.username" # Optional - specify user. Otherwise, uncomment the below -# $targetUser = $Env:UserName +# $targetUser = "example.username" # Optional - specify user. Otherwise, uncomment the below +$targetUser = $Env:UserName $targetUserObject = New-Object System.Security.Principal.NTAccount($targetUser) $targetUserSID = $targetUserObject.Translate([System.Security.Principal.SecurityIdentifier]).value From 42e5d7f491d3c1807b530ac4d2839b0b9217877b Mon Sep 17 00:00:00 2001 From: Peter Rhodes Date: Thu, 23 Sep 2021 09:24:50 +0100 Subject: [PATCH 05/10] Change assumed 'C' drive for :HOMEDRIVE --- install.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/install.ps1 b/install.ps1 index 8f62f1c..bd7d3cd 100644 --- a/install.ps1 +++ b/install.ps1 @@ -19,7 +19,7 @@ $targetUserSID = $targetUserObject.Translate([System.Security.Principal.Security echo "Changing settings for user $targetUser with SID $targetUserSID" -$Env:LocalAppData = "C:\Users\$targetUser\AppData\Local" +$Env:LocalAppData = "$Env:HOMEDRIVE\Users\$targetUser\AppData\Local" $Env:LOCALAPPDATA = $Env:LocalAppData From e753cd8d591a1ddb09a14cef8f4648924e62c9f9 Mon Sep 17 00:00:00 2001 From: Peter Rhodes Date: Thu, 23 Sep 2021 09:26:29 +0100 Subject: [PATCH 06/10] Change hardcoded 'c' for Env:HOMEDRIVE in uninstall.ps1 --- uninstall.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uninstall.ps1 b/uninstall.ps1 index 86a2e94..d431f1d 100644 --- a/uninstall.ps1 +++ b/uninstall.ps1 @@ -16,7 +16,7 @@ $targetUser = "example.username" # Optional - specify user. Otherwise, uncommen $targetUserObject = New-Object System.Security.Principal.NTAccount($targetUser) $targetUserSID = $targetUserObject.Translate([System.Security.Principal.SecurityIdentifier]).value -$Env:LocalAppData = "C:\Users\$targetUser\AppData\Local" +$Env:LocalAppData = "$Env:HOMEDRIVE\Users\$targetUser\AppData\Local" $Env:LOCALAPPDATA = $Env:LocalAppData # Based on @nerdio01's version in https://github.com/microsoft/terminal/issues/1060 From d6c5fc1a5a5fc264c9dc76a23ac9f24cb44215ef Mon Sep 17 00:00:00 2001 From: Peter Rhodes Date: Fri, 24 Sep 2021 09:50:15 +0100 Subject: [PATCH 07/10] Change :HOMEDRIVE for a better way of finding user home directory (Volatile Environment reg keys) --- install.ps1 | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/install.ps1 b/install.ps1 index c9310f5..91c1058 100644 --- a/install.ps1 +++ b/install.ps1 @@ -19,7 +19,9 @@ $targetUserSID = $targetUserObject.Translate([System.Security.Principal.Security echo "Changing settings for user $targetUser with SID $targetUserSID" -$Env:LocalAppData = "$Env:HOMEDRIVE\Users\$targetUser\AppData\Local" +$targetUserHomeDirectory = Get-ItemPropertyValue "Registry::HKEY_USERS\$targetUserSID\Volatile Environment" -Name USERPROFILE + +$Env:LocalAppData = "$targetUserHomeDirectory\$targetUser\AppData\Local" $Env:LOCALAPPDATA = $Env:LocalAppData From 1afc39bea6636c2903613003b66c5b3b1dba2749 Mon Sep 17 00:00:00 2001 From: Peter Rhodes Date: Fri, 24 Sep 2021 11:50:57 +0100 Subject: [PATCH 08/10] Rewrite with function to get desired user --- install.ps1 | 123 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 46 deletions(-) diff --git a/install.ps1 b/install.ps1 index 91c1058..2760781 100644 --- a/install.ps1 +++ b/install.ps1 @@ -9,20 +9,49 @@ param( [Parameter()] [switch] $PreRelease ) +function Get-TargetUser( + [Parameter(Mandatory=$false)] + [string]$defaultTargetUserName) +{ + + if (!$defaultTargetUserName){ $defaultTargetUserName = $Env:UserName } -#Specify target user if different from account running the script, else comment this out -# $targetUser = "example.username" # Optional - specify user. Otherwise, uncomment the below -$targetUser = $Env:UserName + $newTargetUserName = $defaultTargetUserName + $newTargetUserSID = "" + $validUserSelected = $false -$targetUserObject = New-Object System.Security.Principal.NTAccount($targetUser) -$targetUserSID = $targetUserObject.Translate([System.Security.Principal.SecurityIdentifier]).value + DO + { -echo "Changing settings for user $targetUser with SID $targetUserSID" + $userInput = Read-Host "Please enter a username for the installation. Hit to install for $defaultTargetUserName " -$targetUserHomeDirectory = Get-ItemPropertyValue "Registry::HKEY_USERS\$targetUserSID\Volatile Environment" -Name USERPROFILE + if (!$userInput) { + $newTargetUserName = $defaultTargetUserName + } else { + $newTargetUserName = $userInput + } -$Env:LocalAppData = "$targetUserHomeDirectory\$targetUser\AppData\Local" -$Env:LOCALAPPDATA = $Env:LocalAppData + $newTargetUserObject = New-Object System.Security.Principal.NTAccount($newTargetUserName) + try { + $newTargetUserSID = $newTargetUserObject.Translate([System.Security.Principal.SecurityIdentifier]).value + $validUserSelected = $true + } catch { + Write-Warning "Specified user $userInput not found" + } + + } until ($validUserSelected) + + $newTargetUserProfileDirectory = Get-ItemPropertyValue "Registry::HKEY_USERS\$newTargetUserSID\Volatile Environment" -Name USERPROFILE + $newTargetUserAppData = "$newTargetUserProfileDirectory\AppData\Local" + + return [PSCustomObject]@{ + userName = $newTargetUserName + SID = $newTargetUserSID + userProfile = $newTargetUserProfileDirectory + localAppData = $newTargetUserAppData + } + +} function Generate-HelperScript( @@ -238,9 +267,9 @@ function GetActiveProfiles( [bool]$isPreview) { if ($isPreview) { - $file = "$env:LocalAppData\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState\settings.json" + $file = "$targetUser.localAppData\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState\settings.json" } else { - $file = "$env:LocalAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json" + $file = "$targetUser.localAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json" } if (-not (Test-Path $file)) { Write-Error "Couldn't find profiles. Please run Windows Terminal at least once after installing it. Exit." @@ -281,16 +310,16 @@ function GetProfileIcon ( } elseif ($profile.icon -like "ms-appdata:///Roaming/*") { #resolve roaming cache if ($isPreview) { - $profilePng = $icon -replace "ms-appdata:///Roaming", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\RoamingState" -replace "/", "\" + $profilePng = $icon -replace "ms-appdata:///Roaming", "$targetUser.localAppData\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\RoamingState" -replace "/", "\" } else { - $profilePng = $icon -replace "ms-appdata:///Roaming", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\RoamingState" -replace "/", "\" + $profilePng = $icon -replace "ms-appdata:///Roaming", "$targetUser.localAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\RoamingState" -replace "/", "\" } } elseif ($profile.icon -like "ms-appdata:///Local/*") { #resolve local cache if ($isPreview) { - $profilePng = $icon -replace "ms-appdata:///Local", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState" -replace "/", "\" + $profilePng = $icon -replace "ms-appdata:///Local", "$targetUser.localAppData\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState" -replace "/", "\" } else { - $profilePng = $icon -replace "ms-appdata:///Local", "$Env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState" -replace "/", "\" + $profilePng = $icon -replace "ms-appdata:///Local", "$targetUser.localAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState" -replace "/", "\" } } elseif ($profile.icon -like "ms-appx:///*") { # resolve app cache @@ -381,15 +410,15 @@ function CreateProfileMenuItems( $profileIcon = GetProfileIcon $profile $folder $localCache $icon $isPreview if ($layout -eq "Default") { - $rootKey = "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell\$guid" - $rootKeyElevated = "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell\$guid" + $rootKey = "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell\$guid" + $rootKeyElevated = "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell\$guid" CreateMenuItem $rootKey $name $profileIcon $command $false CreateMenuItem $rootKeyElevated $name $profileIcon $elevated $true } elseif ($layout -eq "Flat") { - CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false - CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true - CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false - CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true + CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false + CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true + CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false + CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true } } @@ -402,7 +431,7 @@ function CreateMenuItems( [bool]$includePreview) { $folder = GetProgramFilesFolder $includePreview - $localCache = "$Env:LOCALAPPDATA\Microsoft\WindowsApps\Cache" + $localCache = "$targetUser.localAppData\Microsoft\WindowsApps\Cache" if (-not (Test-Path $localCache)) { New-Item $localCache -ItemType Directory | Out-Null @@ -413,36 +442,36 @@ function CreateMenuItems( if ($layout -eq "Default") { # defaut layout creates two menus - New-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Force | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'Icon' -PropertyType String -Value $icon | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null + New-Item -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Force | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'Icon' -PropertyType String -Value $icon | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null - New-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Force | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'Icon' -PropertyType String -Value $icon | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null + New-Item -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Force | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'Icon' -PropertyType String -Value $icon | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null - New-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell" -Force | Out-Null + New-Item -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell" -Force | Out-Null - New-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Force | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'Icon' -PropertyType String -Value $icon | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null + New-Item -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Force | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'Icon' -PropertyType String -Value $icon | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null - New-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Force | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'Icon' -PropertyType String -Value $icon | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null + New-Item -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Force | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'Icon' -PropertyType String -Value $icon | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null - New-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell" -Force | Out-Null + New-Item -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell" -Force | Out-Null } elseif ($layout -eq "Mini") { $command = """$executable"" -d ""%V.""" $elevated = "wscript.exe ""$localCache/helper.vbs"" ""$executable"" ""%V.""" - CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false - CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true - CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false - CreateMenuItem "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true + CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false + CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true + CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false + CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true return } @@ -461,7 +490,7 @@ if ((Get-Process -Id $pid).Path -like "*WindowsApps*") { } #if ((Test-Path "Registry::HKEY_CLASSES_ROOT\Directory\shell\MenuTerminal") -and -# -not (Test-Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal")) { +# -not (Test-Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminal")) { # Write-Error "Please execute uninstall.old.ps1 to remove previous installation." # exit 1 #} @@ -471,7 +500,7 @@ if ($PSVersionTable.PSVersion.Major -lt 6) { exit 1 } -$executable = "$Env:LOCALAPPDATA\Microsoft\WindowsApps\wt.exe" +$executable = "$targetUser.localAppData\Microsoft\WindowsApps\wt.exe" if (-not (Test-Path $executable)) { Write-Error "Windows Terminal not detected at $executable. Learn how to install it from https://github.com/microsoft/terminal (via Microsoft Store is recommended). Exit." exit 1 @@ -479,6 +508,8 @@ if (-not (Test-Path $executable)) { Write-Host "Use $Layout layout." +$targetUser = Get-TargetUser + CreateMenuItems $executable $Layout $PreRelease Write-Host "Windows Terminal installed to Windows Explorer context menu." From 72f65a534661e25375162b4a7c84cdf0e6970f19 Mon Sep 17 00:00:00 2001 From: Peter Rhodes Date: Fri, 24 Sep 2021 13:40:51 +0100 Subject: [PATCH 09/10] Change to Get-TargetUserfunction which prompts user --- install.ps1 | 89 ++++++++++++++++++++++++++++------------------------- 1 file changed, 47 insertions(+), 42 deletions(-) diff --git a/install.ps1 b/install.ps1 index 2760781..74d0e62 100644 --- a/install.ps1 +++ b/install.ps1 @@ -41,19 +41,26 @@ function Get-TargetUser( } until ($validUserSelected) - $newTargetUserProfileDirectory = Get-ItemPropertyValue "Registry::HKEY_USERS\$newTargetUserSID\Volatile Environment" -Name USERPROFILE - $newTargetUserAppData = "$newTargetUserProfileDirectory\AppData\Local" + if ($newTargetUserName -eq $Env:UserName){ + $newTargetUserProfileDirectory = $env:USERPROFILE + $newTargetUserLocalAppData = $env:LOCALAPPDATA + } else { + $newTargetUserProfileDirectory = Get-ItemPropertyValue "Registry::HKEY_USERS\$newTargetUserSID\Volatile Environment" -Name USERPROFILE + $newTargetUserLocalAppData = Get-ItemPropertyValue "Registry::HKEY_USERS\$newTargetUserSID\Volatile Environment" -Name LOCALAPPDATA + } return [PSCustomObject]@{ userName = $newTargetUserName SID = $newTargetUserSID userProfile = $newTargetUserProfileDirectory - localAppData = $newTargetUserAppData + localAppData = $newTargetUserLocalAppData } } +$targetUser = Get-TargetUser + function Generate-HelperScript( # The cache folder [Parameter(Mandatory=$true)] @@ -267,9 +274,9 @@ function GetActiveProfiles( [bool]$isPreview) { if ($isPreview) { - $file = "$targetUser.localAppData\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState\settings.json" + $file = "$($targetUser.localAppData)\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState\settings.json" } else { - $file = "$targetUser.localAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json" + $file = "$($targetUser.localAppData)\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json" } if (-not (Test-Path $file)) { Write-Error "Couldn't find profiles. Please run Windows Terminal at least once after installing it. Exit." @@ -310,16 +317,16 @@ function GetProfileIcon ( } elseif ($profile.icon -like "ms-appdata:///Roaming/*") { #resolve roaming cache if ($isPreview) { - $profilePng = $icon -replace "ms-appdata:///Roaming", "$targetUser.localAppData\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\RoamingState" -replace "/", "\" + $profilePng = $icon -replace "ms-appdata:///Roaming", "$($targetUser.localAppData)\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\RoamingState" -replace "/", "\" } else { - $profilePng = $icon -replace "ms-appdata:///Roaming", "$targetUser.localAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\RoamingState" -replace "/", "\" + $profilePng = $icon -replace "ms-appdata:///Roaming", "$($targetUser.localAppData)\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\RoamingState" -replace "/", "\" } } elseif ($profile.icon -like "ms-appdata:///Local/*") { #resolve local cache if ($isPreview) { - $profilePng = $icon -replace "ms-appdata:///Local", "$targetUser.localAppData\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState" -replace "/", "\" + $profilePng = $icon -replace "ms-appdata:///Local", "$($targetUser.localAppData)\Packages\Microsoft.WindowsTerminalPreview_8wekyb3d8bbwe\LocalState" -replace "/", "\" } else { - $profilePng = $icon -replace "ms-appdata:///Local", "$targetUser.localAppData\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState" -replace "/", "\" + $profilePng = $icon -replace "ms-appdata:///Local", "$($targetUser.localAppData)\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState" -replace "/", "\" } } elseif ($profile.icon -like "ms-appx:///*") { # resolve app cache @@ -410,15 +417,15 @@ function CreateProfileMenuItems( $profileIcon = GetProfileIcon $profile $folder $localCache $icon $isPreview if ($layout -eq "Default") { - $rootKey = "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell\$guid" - $rootKeyElevated = "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell\$guid" + $rootKey = "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell\$guid" + $rootKeyElevated = "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell\$guid" CreateMenuItem $rootKey $name $profileIcon $command $false CreateMenuItem $rootKeyElevated $name $profileIcon $elevated $true } elseif ($layout -eq "Flat") { - CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false - CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true - CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false - CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true + CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false + CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true + CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal_$guid" "$name here" $profileIcon $command $false + CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin_$guid" "$name here as administrator" $profileIcon $elevated $true } } @@ -431,7 +438,7 @@ function CreateMenuItems( [bool]$includePreview) { $folder = GetProgramFilesFolder $includePreview - $localCache = "$targetUser.localAppData\Microsoft\WindowsApps\Cache" + $localCache = "$($targetUser.localAppData)\Microsoft\WindowsApps\Cache" if (-not (Test-Path $localCache)) { New-Item $localCache -ItemType Directory | Out-Null @@ -442,36 +449,36 @@ function CreateMenuItems( if ($layout -eq "Default") { # defaut layout creates two menus - New-Item -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Force | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'Icon' -PropertyType String -Value $icon | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null + New-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Force | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'Icon' -PropertyType String -Value $icon | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null - New-Item -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Force | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'Icon' -PropertyType String -Value $icon | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null + New-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Force | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here' | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'Icon' -PropertyType String -Value $icon | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminal' | Out-Null - New-Item -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell" -Force | Out-Null + New-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell" -Force | Out-Null - New-Item -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Force | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'Icon' -PropertyType String -Value $icon | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null + New-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Force | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'Icon' -PropertyType String -Value $icon | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null - New-Item -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Force | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'Icon' -PropertyType String -Value $icon | Out-Null - New-ItemProperty -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null + New-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Force | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'MUIVerb' -PropertyType String -Value 'Windows Terminal here as administrator' | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'Icon' -PropertyType String -Value $icon | Out-Null + New-ItemProperty -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Name 'ExtendedSubCommandsKey' -PropertyType String -Value 'Directory\\ContextMenus\\MenuTerminalAdmin' | Out-Null - New-Item -Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell" -Force | Out-Null + New-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell" -Force | Out-Null } elseif ($layout -eq "Mini") { $command = """$executable"" -d ""%V.""" $elevated = "wscript.exe ""$localCache/helper.vbs"" ""$executable"" ""%V.""" - CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false - CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true - CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false - CreateMenuItem "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true + CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false + CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true + CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini" "Windows Terminal here" $icon $command $false + CreateMenuItem "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdminMini" "Windows Terminal here as administrator" $icon $elevated $true return } @@ -490,7 +497,7 @@ if ((Get-Process -Id $pid).Path -like "*WindowsApps*") { } #if ((Test-Path "Registry::HKEY_CLASSES_ROOT\Directory\shell\MenuTerminal") -and -# -not (Test-Path "Registry::HKEY_USERS\$targetUser.SID\SOFTWARE\Classes\Directory\shell\MenuTerminal")) { +# -not (Test-Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminal")) { # Write-Error "Please execute uninstall.old.ps1 to remove previous installation." # exit 1 #} @@ -500,7 +507,7 @@ if ($PSVersionTable.PSVersion.Major -lt 6) { exit 1 } -$executable = "$targetUser.localAppData\Microsoft\WindowsApps\wt.exe" +$executable = "$($targetUser.localAppData)\Microsoft\WindowsApps\wt.exe" if (-not (Test-Path $executable)) { Write-Error "Windows Terminal not detected at $executable. Learn how to install it from https://github.com/microsoft/terminal (via Microsoft Store is recommended). Exit." exit 1 @@ -508,8 +515,6 @@ if (-not (Test-Path $executable)) { Write-Host "Use $Layout layout." -$targetUser = Get-TargetUser - CreateMenuItems $executable $Layout $PreRelease Write-Host "Windows Terminal installed to Windows Explorer context menu." From 4506e1a185852c3858834872ab31eae0b97a5737 Mon Sep 17 00:00:00 2001 From: Peter Rhodes Date: Fri, 24 Sep 2021 13:59:20 +0100 Subject: [PATCH 10/10] Refactor to put Get-TargetUser fn into importable module shared by install and uninstall scripts --- TargetUser.psm1 | 49 +++++++++++++++++++++++++++++++++++++++++++++ install.ps1 | 53 +++---------------------------------------------- uninstall.ps1 | 34 +++++++++++++------------------ 3 files changed, 66 insertions(+), 70 deletions(-) create mode 100644 TargetUser.psm1 diff --git a/TargetUser.psm1 b/TargetUser.psm1 new file mode 100644 index 0000000..fdcb467 --- /dev/null +++ b/TargetUser.psm1 @@ -0,0 +1,49 @@ + +function Get-TargetUser( + [Parameter(Mandatory=$false)] + [string]$defaultTargetUserName) +{ + + if (!$defaultTargetUserName){ $defaultTargetUserName = $Env:UserName } + + $newTargetUserName = $defaultTargetUserName + $newTargetUserSID = "" + $validUserSelected = $false + + DO + { + + $userInput = Read-Host "Please enter a username for the installation. Hit to install for $defaultTargetUserName " + + if (!$userInput) { + $newTargetUserName = $defaultTargetUserName + } else { + $newTargetUserName = $userInput + } + + $newTargetUserObject = New-Object System.Security.Principal.NTAccount($newTargetUserName) + try { + $newTargetUserSID = $newTargetUserObject.Translate([System.Security.Principal.SecurityIdentifier]).value + $validUserSelected = $true + } catch { + Write-Warning "Specified user $userInput not found" + } + + } until ($validUserSelected) + + if ($newTargetUserName -eq $Env:UserName){ + $newTargetUserProfileDirectory = $env:USERPROFILE + $newTargetUserLocalAppData = $env:LOCALAPPDATA + } else { + $newTargetUserProfileDirectory = Get-ItemPropertyValue "Registry::HKEY_USERS\$newTargetUserSID\Volatile Environment" -Name USERPROFILE + $newTargetUserLocalAppData = Get-ItemPropertyValue "Registry::HKEY_USERS\$newTargetUserSID\Volatile Environment" -Name LOCALAPPDATA + } + + return [PSCustomObject]@{ + userName = $newTargetUserName + SID = $newTargetUserSID + userProfile = $newTargetUserProfileDirectory + localAppData = $newTargetUserLocalAppData + } + +} diff --git a/install.ps1 b/install.ps1 index 74d0e62..833f834 100644 --- a/install.ps1 +++ b/install.ps1 @@ -9,59 +9,12 @@ param( [Parameter()] [switch] $PreRelease ) -function Get-TargetUser( - [Parameter(Mandatory=$false)] - [string]$defaultTargetUserName) -{ - - if (!$defaultTargetUserName){ $defaultTargetUserName = $Env:UserName } - - $newTargetUserName = $defaultTargetUserName - $newTargetUserSID = "" - $validUserSelected = $false - - DO - { - - $userInput = Read-Host "Please enter a username for the installation. Hit to install for $defaultTargetUserName " - - if (!$userInput) { - $newTargetUserName = $defaultTargetUserName - } else { - $newTargetUserName = $userInput - } - - $newTargetUserObject = New-Object System.Security.Principal.NTAccount($newTargetUserName) - try { - $newTargetUserSID = $newTargetUserObject.Translate([System.Security.Principal.SecurityIdentifier]).value - $validUserSelected = $true - } catch { - Write-Warning "Specified user $userInput not found" - } - - } until ($validUserSelected) - - if ($newTargetUserName -eq $Env:UserName){ - $newTargetUserProfileDirectory = $env:USERPROFILE - $newTargetUserLocalAppData = $env:LOCALAPPDATA - } else { - $newTargetUserProfileDirectory = Get-ItemPropertyValue "Registry::HKEY_USERS\$newTargetUserSID\Volatile Environment" -Name USERPROFILE - $newTargetUserLocalAppData = Get-ItemPropertyValue "Registry::HKEY_USERS\$newTargetUserSID\Volatile Environment" -Name LOCALAPPDATA - } - - return [PSCustomObject]@{ - userName = $newTargetUserName - SID = $newTargetUserSID - userProfile = $newTargetUserProfileDirectory - localAppData = $newTargetUserLocalAppData - } - -} +Import-Module .\TargetUser.psm1 $targetUser = Get-TargetUser -function Generate-HelperScript( +function New-HelperScript( # The cache folder [Parameter(Mandatory=$true)] [string]$cache) @@ -444,7 +397,7 @@ function CreateMenuItems( New-Item $localCache -ItemType Directory | Out-Null } - Generate-HelperScript $localCache + New-HelperScript $localCache $icon = GetWindowsTerminalIcon $folder $localCache if ($layout -eq "Default") { diff --git a/uninstall.ps1 b/uninstall.ps1 index 29e3685..fbd250c 100644 --- a/uninstall.ps1 +++ b/uninstall.ps1 @@ -9,15 +9,9 @@ param( [switch] $PreRelease ) -#Specify target user if different from account running the script, else comment this out -# $targetUser = "example.username" # Optional - specify user. Otherwise, uncomment the below -$targetUser = $Env:UserName +Import-Module .\TargetUser.psm1 -$targetUserObject = New-Object System.Security.Principal.NTAccount($targetUser) -$targetUserSID = $targetUserObject.Translate([System.Security.Principal.SecurityIdentifier]).value - -$Env:LocalAppData = "$Env:HOMEDRIVE\Users\$targetUser\AppData\Local" -$Env:LOCALAPPDATA = $Env:LocalAppData +$targetUser = Get-TargetUser # Based on @nerdio01's version in https://github.com/microsoft/terminal/issues/1060 @@ -27,12 +21,12 @@ if ((Get-Process -Id $pid).Path -like "*WindowsApps*") { } if ((Test-Path "Registry::HKEY_CLASSES_ROOT\Directory\shell\MenuTerminal") -and - -not (Test-Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal")) { + -not (Test-Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminal")) { Write-Error "Please execute uninstall.old.ps1 to remove previous installation." exit 1 } -$localCache = "$Env:LOCALAPPDATA\Microsoft\WindowsApps\Cache" +$localCache = "$($targetUser.localAppData)\Microsoft\WindowsApps\Cache" if (Test-Path $localCache) { Remove-Item $localCache -Recurse } @@ -40,12 +34,12 @@ if (Test-Path $localCache) { Write-Host "Use" $layout "layout." if ($layout -eq "Default") { - Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell" -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminal" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminal" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminal\shell" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalAdmin" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdmin" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\ContextMenus\MenuTerminalAdmin\shell" -Recurse -ErrorAction Ignore | Out-Null } elseif ($layout -eq "Flat") { $rootKey = 'HKEY_CURRENT_USER\SOFTWARE\Classes\Directory\shell' foreach ($key in Get-ChildItem -Path "Registry::$rootKey") { @@ -61,10 +55,10 @@ if ($layout -eq "Default") { } } } elseif ($layout -eq "Mini") { - Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalMini" -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\shell\MenuTerminalAdminMini" -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini" -Recurse -ErrorAction Ignore | Out-Null - Remove-Item -Path "Registry::HKEY_USERS\$targetUserSID\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdminMini" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalMini" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\shell\MenuTerminalAdminMini" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalMini" -Recurse -ErrorAction Ignore | Out-Null + Remove-Item -Path "Registry::HKEY_USERS\$($targetUser.SID)\SOFTWARE\Classes\Directory\Background\shell\MenuTerminalAdminMini" -Recurse -ErrorAction Ignore | Out-Null } Write-Host "Windows Terminal uninstalled from Windows Explorer context menu."