Is there a Powershell cmdlet or script to query Active Directory if a given domain account (such as "myDomain\myUser") exists?
Is there a Powershell cmdlet or script to query Active Directory if a given domain account (such as "myDomain\myUser") exists?
This is what we use to validate accounts. It relies of course on Import-Module ActiveDirectory and either a 2008 R2 DC, or a DC running ADWS:
Old question, I know, but I feel I need to add this bit here because none of the previous answers use any form of error handling.
Also, if you need to support users from multiple domains, you will have to query the correct domain controller (or query the Global Catalog and specify the DN of a directory partition).
You can use the Directory Searcher .net object to do this.
Here is a very un-optomized code snippet from one of my utility scripts that has fallen by the wayside.
So you should be able to change the
.objectclass
to.cn
or.name
and then match against that.Or don't be lazy like I am and read up on how to construct a proper query :)
I would grab the Quest AD Roles cmdlets - http://www.quest.com/powershell/activeroles-server.aspx - and user their Get-QADUser cmdlet with the -Identity parameter.
For example,