Get-ADUser quirk

Came across an interesting quirk of the way Get-ADUser works.

If you use the –Identity parameter and tell it to find a specific user

PS> Get-ADUser -Identity dontexist
Get-ADUser : Cannot find an object with identity: ‘dontexist’ under: ‘DC=Manticore,DC=org’.
At line:1 char:1
+ Get-ADUser -Identity dontexist
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (dontexist:ADUser) [Get-ADUser], ADIdentityNotFoundException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

You get an error if the user can’t be found

If you use a –Filter to perform the same search – its quite OK for nothing to be returned

PS> Get-ADUser -Filter {SamAccountName -eq ‘dontexist’}
PS>

Something to be aware of when error handling. When using the filter you can’t use try-catch because there’s no error if there’s no result

This entry was posted in PowerShell and Active Directory. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s