From e262af617d7e90f8907b098ed611232857ab68ad Mon Sep 17 00:00:00 2001 From: Martin Wiethan <47688561+Marterich@users.noreply.github.com> Date: Tue, 9 Jul 2024 19:36:06 +0200 Subject: [PATCH] Handle Computers without Windows Terminal (eg Win10) --- functions/public/Invoke-WPFTweakPS7.ps1 | 41 +++++++++++++++---------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/functions/public/Invoke-WPFTweakPS7.ps1 b/functions/public/Invoke-WPFTweakPS7.ps1 index 85ab9108..209b71ce 100644 --- a/functions/public/Invoke-WPFTweakPS7.ps1 +++ b/functions/public/Invoke-WPFTweakPS7.ps1 @@ -25,22 +25,29 @@ function Invoke-WPFTweakPS7{ $targetTerminalName = "Windows PowerShell" } } - - $settingsPath = "$env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json" - if (Test-Path -Path $settingsPath) { - Write-Host "Settings file found." - $settingsContent = Get-Content -Path $settingsPath | ConvertFrom-Json - $ps7Profile = $settingsContent.profiles.list | Where-Object { $_.name -eq $targetTerminalName } - if ($ps7Profile) { - $settingsContent.defaultProfile = $ps7Profile.guid - $updatedSettings = $settingsContent | ConvertTo-Json -Depth 100 - Set-Content -Path $settingsPath -Value $updatedSettings - Write-Host "Default profile updated to $targetTerminalName using the name attribute." - } else { - Write-Host "No PowerShell 7 profile found in Windows Terminal settings using the name attribute." - } - } else { - Write-Host "Settings file not found at $settingsPath" + # Check if the Windows Terminal is installed and return if not (Prerequisite for the following code) + if (-not (Get-Command "wt" -ErrorAction SilentlyContinue)){ + Write-Host "Windows Terminal not installed. Skipping Terminal preference" + return + } + # Check if the Windows Terminal settings.json file exists and return if not (Prereqisite for the following code) + $settingsPath = "$env:LOCALAPPDATA\Packages\Microsoft.WindowsTerminal_8wekyb3d8bbwe\LocalState\settings.json" + if (-not (Test-Path -Path $settingsPath)){ + Write-Host "Windows Terminal Settings file not found at $settingsPath" + return } -} + Write-Host "Settings file found." + $settingsContent = Get-Content -Path $settingsPath | ConvertFrom-Json + $ps7Profile = $settingsContent.profiles.list | Where-Object { $_.name -eq $targetTerminalName } + if ($ps7Profile) { + $settingsContent.defaultProfile = $ps7Profile.guid + $updatedSettings = $settingsContent | ConvertTo-Json -Depth 100 + Set-Content -Path $settingsPath -Value $updatedSettings + Write-Host "Default profile updated to " -NoNewline + Write-Host "$targetTerminalName " -ForegroundColor White -NoNewline + Write-Host "using the name attribute." + } else { + Write-Host "No PowerShell 7 profile found in Windows Terminal settings using the name attribute." + } +}