Files
Win11Debloat/Scripts/Helpers/Test-TargetUserName.ps1
T

51 lines
1.7 KiB
PowerShell
Raw Normal View History

<#
.SYNOPSIS
Validates a username for the "apply changes to another user" deployment target.
.DESCRIPTION
Rejects an empty name, the current user's own name (that's the "Current User" option
instead), and a name with no matching local profile, in that order.
.OUTPUTS
PSCustomObject with IsValid (bool), UserName (the trimmed input), and Message (a
translated string describing the validation result, success or failure alike).
#>
2026-05-08 21:19:52 +02:00
function Test-TargetUserName {
param(
[AllowNull()]
[AllowEmptyString()]
[string]$UserName
)
$normalizedUserName = if ($null -ne $UserName) { $UserName.Trim() } else { '' }
if ([string]::IsNullOrWhiteSpace($normalizedUserName)) {
return [PSCustomObject]@{
IsValid = $false
UserName = $normalizedUserName
Message = Get-Translation -Key 'UsernameValidationEmpty'
2026-05-08 21:19:52 +02:00
}
}
if (Test-UserNameMatch -UserNameA $normalizedUserName -UserNameB $env:USERNAME) {
2026-05-08 21:19:52 +02:00
return [PSCustomObject]@{
IsValid = $false
UserName = $normalizedUserName
Message = Get-Translation -Key 'UsernameValidationOwnUsername'
2026-05-08 21:19:52 +02:00
}
}
if (-not (Test-UserProfileExists -userName $normalizedUserName)) {
2026-05-08 21:19:52 +02:00
return [PSCustomObject]@{
IsValid = $false
UserName = $normalizedUserName
Message = Get-Translation -Key 'UsernameValidationNotFound'
2026-05-08 21:19:52 +02:00
}
}
return [PSCustomObject]@{
IsValid = $true
UserName = $normalizedUserName
Message = Get-Translation -Key 'UsernameValidationFound' -FormatArgs @($normalizedUserName)
2026-05-08 21:19:52 +02:00
}
}