diff --git a/Scripts/GUI/Show-MainWindow.ps1 b/Scripts/GUI/Show-MainWindow.ps1 index 035f906..d27bd15 100644 --- a/Scripts/GUI/Show-MainWindow.ps1 +++ b/Scripts/GUI/Show-MainWindow.ps1 @@ -1283,7 +1283,7 @@ function Show-MainWindow { $appRemovalScopeCombo.SelectedIndex = 0 } 1 { - $userSelectionDescription.Text = "Changes will be applied to a different user profile on this system. Note: changes may not apply correctly if the target user is currently logged in." + $userSelectionDescription.Text = "Changes will be applied to a different user profile on this system." $otherUserPanel.Visibility = 'Visible' $usernameValidationMessage.Text = "" # Hide "Current user only" option, show "Target user only" option @@ -1353,13 +1353,13 @@ function Show-MainWindow { $successBrush = $window.Resources['ValidationSuccessColor'] if ($username.Length -eq 0) { - $usernameValidationMessage.Text = "[X] Please enter a username" + $usernameValidationMessage.Text = "Please enter a username" $usernameValidationMessage.Foreground = $errorBrush return $false } if ($username -eq $env:USERNAME) { - $usernameValidationMessage.Text = "[X] Cannot enter your own username, use 'Current User' option instead" + $usernameValidationMessage.Text = "Cannot enter your own username, use 'Current User' option instead" $usernameValidationMessage.Foreground = $errorBrush return $false } @@ -1367,12 +1367,18 @@ function Show-MainWindow { $userExists = CheckIfUserExists -Username $username if ($userExists) { - $usernameValidationMessage.Text = "[OK] User found: $username" + if (TestIfUserIsLoggedIn -Username $username) { + $usernameValidationMessage.Text = "User '$username' is currently logged in. Please sign out that user first." + $usernameValidationMessage.Foreground = $errorBrush + return $false + } + + $usernameValidationMessage.Text = "User found: $username" $usernameValidationMessage.Foreground = $successBrush return $true } - $usernameValidationMessage.Text = "[X] User not found, please enter a valid username" + $usernameValidationMessage.Text = "User not found, please enter a valid username" $usernameValidationMessage.Foreground = $errorBrush return $false } @@ -1515,7 +1521,13 @@ function Show-MainWindow { $deploymentApplyBtn = $window.FindName('DeploymentApplyBtn') $deploymentApplyBtn.Add_Click({ if (-not (ValidateOtherUsername)) { - Show-MessageBox -Message "Please enter a valid username." -Title "Invalid Username" -Button 'OK' -Icon 'Warning' | Out-Null + $validationMessage = if (-not [string]::IsNullOrWhiteSpace($usernameValidationMessage.Text)) { + $usernameValidationMessage.Text + } + else { + "Please enter a valid username." + } + Show-MessageBox -Message $validationMessage -Title "Invalid Username" -Button 'OK' -Icon 'Warning' | Out-Null return } diff --git a/Scripts/Helpers/TestIfUserIsLoggedIn.ps1 b/Scripts/Helpers/TestIfUserIsLoggedIn.ps1 new file mode 100644 index 0000000..9c0d759 --- /dev/null +++ b/Scripts/Helpers/TestIfUserIsLoggedIn.ps1 @@ -0,0 +1,42 @@ +function TestIfUserIsLoggedIn { + param( + [Parameter(Mandatory)] + [string]$Username + ) + + try { + $quserOutput = @(& quser 2>$null) + if ($LASTEXITCODE -ne 0 -or -not $quserOutput) { + return $false + } + + foreach ($line in ($quserOutput | Select-Object -Skip 1)) { + if ([string]::IsNullOrWhiteSpace($line)) { continue } + + # Remove current-session marker and split columns. + $normalizedLine = $line.TrimStart('>', ' ') + $parts = $normalizedLine -split '\s+' + if ($parts.Count -eq 0) { continue } + + $sessionUser = $parts[0] + if ([string]::IsNullOrWhiteSpace($sessionUser)) { continue } + + # Normalize possible DOMAIN\user or user@domain formats. + if ($sessionUser.Contains('\')) { + $sessionUser = ($sessionUser -split '\\')[-1] + } + if ($sessionUser.Contains('@')) { + $sessionUser = ($sessionUser -split '@')[0] + } + + if ($sessionUser.Equals($Username, [System.StringComparison]::OrdinalIgnoreCase)) { + return $true + } + } + } + catch { + return $false + } + + return $false +} diff --git a/Win11Debloat.ps1 b/Win11Debloat.ps1 index 270a47d..b39b022 100644 --- a/Win11Debloat.ps1 +++ b/Win11Debloat.ps1 @@ -278,6 +278,7 @@ if (-not $script:WingetInstalled -and -not $Silent) { . "$PSScriptRoot/Scripts/Helpers/GetTargetUserForAppRemoval.ps1" . "$PSScriptRoot/Scripts/Helpers/GetUserDirectory.ps1" . "$PSScriptRoot/Scripts/Helpers/GetUserName.ps1" +. "$PSScriptRoot/Scripts/Helpers/TestIfUserIsLoggedIn.ps1" # Threading functions . "$PSScriptRoot/Scripts/Threading/DoEvents.ps1"