2026-03-15 22:58:06 +01:00
|
|
|
function CheckIfUserExists {
|
|
|
|
|
param (
|
2026-05-06 15:54:03 +02:00
|
|
|
[string]$userName
|
2026-03-15 22:58:06 +01:00
|
|
|
)
|
|
|
|
|
|
2026-05-06 15:54:03 +02:00
|
|
|
if ([string]::IsNullOrWhiteSpace($userName)) {
|
2026-03-15 22:58:06 +01:00
|
|
|
return $false
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 15:54:03 +02:00
|
|
|
$lookupName = $userName.Trim()
|
|
|
|
|
|
|
|
|
|
# Validate special characters against the local username segment (user in DOMAIN\user or user@domain).
|
|
|
|
|
$localUserName = GetLocalUserNameSegment -UserName $lookupName
|
|
|
|
|
|
|
|
|
|
if ($localUserName.IndexOfAny([System.IO.Path]::GetInvalidFileNameChars()) -ge 0) {
|
|
|
|
|
return $false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
# PowerShell treats [] as wildcard chars in non-literal paths; disallow them explicitly.
|
|
|
|
|
if ($localUserName -match '[\[\]]') {
|
2026-03-15 22:58:06 +01:00
|
|
|
return $false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
2026-05-06 15:54:03 +02:00
|
|
|
$userContext = ResolveUserProfileContext -UserName $lookupName
|
|
|
|
|
if (-not $userContext -or [string]::IsNullOrWhiteSpace($userContext.ProfilePath)) {
|
|
|
|
|
return $false
|
|
|
|
|
}
|
2026-03-15 22:58:06 +01:00
|
|
|
|
2026-05-06 15:54:03 +02:00
|
|
|
if ($lookupName -ieq 'Default') {
|
2026-03-15 22:58:06 +01:00
|
|
|
return $true
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-06 15:54:03 +02:00
|
|
|
return -not [string]::IsNullOrWhiteSpace($userContext.UserSid)
|
2026-03-15 22:58:06 +01:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
catch {
|
2026-05-06 15:54:03 +02:00
|
|
|
Write-Error "Something went wrong when trying to find the user directory path for user $lookupName. Please ensure the user exists on this system"
|
2026-03-15 22:58:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $false
|
|
|
|
|
}
|