Created
June 21, 2018 13:29
-
-
Save 45413/92ed95a9734acd8b07a883854d8d2cc1 to your computer and use it in GitHub Desktop.
Validate the existence of an AD User with powershell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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