Skip to content

Instantly share code, notes, and snippets.

@45413
Created June 21, 2018 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 45413/92ed95a9734acd8b07a883854d8d2cc1 to your computer and use it in GitHub Desktop.
Save 45413/92ed95a9734acd8b07a883854d8d2cc1 to your computer and use it in GitHub Desktop.
Validate the existence of an AD User with powershell
# Check if AD User Exist: Method 1
[bool] (Get-ADUser -Filter {SamAccountName -eq "NonExistingADUser" })
# Check if AD User Exist: Method 2
## supports all valid identity formats
function Test-ADUser {
[CmdletBinding()]
param (
# Identity
[Parameter(Mandatory=$true)]
[Alias("User")]
[string]
$Identity
)
try {
Get-ADUser -Identity $Identity -ErrorAction Stop | out-null
return $true
}
catch {
return $false
}
}
Test-ADUser -Identity "NonExistingADUser"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment