From 86a2555454fe38f648949d1ed550306a77fefd32 Mon Sep 17 00:00:00 2001 From: Raphire <9938813+Raphire@users.noreply.github.com> Date: Sun, 28 Sep 2025 20:40:24 +0200 Subject: [PATCH] Fix issue where clearing/replacing the start menu pinned apps would fail and crash under certain circumstances, part 2 --- Win11Debloat.ps1 | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/Win11Debloat.ps1 b/Win11Debloat.ps1 index 309833e..7099c05 100644 --- a/Win11Debloat.ps1 +++ b/Win11Debloat.ps1 @@ -582,18 +582,23 @@ function GetUserDirectory { $exitIfPathNotFound = $true ) - $userDirectoryExists = Test-Path "$env:SystemDrive\Users\$userName" - $userPath = "$env:SystemDrive\Users\$userName\$fileName" - - if ((Test-Path $userPath) -or ($userDirectoryExists -and (-not $exitIfPathNotFound))) { - return $userPath - } - - $userDirectoryExists = Test-Path $env:USERPROFILE -Replace ('\\' + $env:USERNAME + '$'), "\$userName" - $userPath = $env:USERPROFILE -Replace ('\\' + $env:USERNAME + '$'), "\$userName\$fileName" - - if ((Test-Path $userPath) -or ($userDirectoryExists -and (-not $exitIfPathNotFound))) { - return $userPath + try { + $userDirectoryExists = Test-Path "$env:SystemDrive\Users\$userName" + $userPath = "$env:SystemDrive\Users\$userName\$fileName" + + if ((Test-Path $userPath) -or ($userDirectoryExists -and (-not $exitIfPathNotFound))) { + return $userPath + } + + $userDirectoryExists = Test-Path ($env:USERPROFILE -Replace ('\\' + $env:USERNAME + '$'), "\$userName") + $userPath = $env:USERPROFILE -Replace ('\\' + $env:USERNAME + '$'), "\$userName\$fileName" + + if ((Test-Path $userPath) -or ($userDirectoryExists -and (-not $exitIfPathNotFound))) { + return $userPath + } + } catch { + Write-Host "Error: Something went wrong when trying to find the user directory path for user $userName. Please ensure the user exists on this system." -ForegroundColor Red + AwaitKeyToExit } Write-Host "Error: Unable to find user directory path for user $userName" -ForegroundColor Red @@ -738,6 +743,7 @@ function ReplaceStartMenu { Move-Item -Path $startMenuBinFile -Destination $backupBinFile -Force } else { Write-Host "Warning: Unable to find original start2.bin file for user $userName. No backup was created for this user!" -ForegroundColor Yellow + New-Item -ItemType File -Path $startMenuBinFile -Force } # Copy template file