I have a bit of code that looks like this:
if (Get-ADUser $DN -EA SilentlyContinue) {
# Exists
} else {
# Doesn't Exist
}
Unfortunately, when Get-ADUser the DN fails to find a user (which is fine, it means the object name is not taken), it throws up and spits out an error. I know it will fail, that's fine, which is why I have an -ErrorAction
to SilentlyContinue
. Unfortunately it seems to do nothing... I still get barf on the script output. The code works, it's just ugly due to the console spitting out the error.
The only way I have found to be working without spitting an error is with the filter parameter:
if (Get-ADUser -Filter {distinguishedName -eq $DN} ) {
# Exists
} else {
# Doesn't Exist
}