Monthly Archive

Categories

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

Comments are closed.