function CheckIfUserExists { param ( $userName ) if ($userName -match '[<>:"|?*]') { return $false } if ([string]::IsNullOrWhiteSpace($userName)) { return $false } try { $userExists = Test-Path "$env:SystemDrive\Users\$userName" if ($userExists) { return $true } $userExists = Test-Path ($env:USERPROFILE -Replace ('\\' + $env:USERNAME + '$'), "\$userName") if ($userExists) { return $true } } catch { Write-Error "Something went wrong when trying to find the user directory path for user $userName. Please ensure the user exists on this system" } return $false }