#PSTip Validate if a user exists in Active Directory

Note: This tip requires PowerShell 3.0 or above.

[adsisearcher] is a PowerShell type adapter for DirectorySearcher .NET class. We can use this type adapter to perform queries against Active Directory Domain Services.

In this tip, we shall see how we can validate if a given user exists in AD or not. This is how we do it.

[bool]([adsisearcher]"samaccountname=username").FindOne()

This snippet returns True or False based on if the user exists or not.

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts

%d bloggers like this: