From 1afc39bea6636c2903613003b66c5b3b1dba2749 Mon Sep 17 00:00:00 2001 From: Peter Rhodes Date: Fri, 24 Sep 2021 11:50:57 +0100 Subject: [PATCH 1/3] 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 2/3] 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 3/3] 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."