0

I have a list of team members names - in a first_name, last_name format in a CSV. How can I look up their names in AD using Powershell - so that I can get their domain userids ?

I saw examples where "adsi" was used to look based on domain userid->full-name, but I need to perform this the other way round.

(For instance: Retrieve current domain user's full name)

I'm not admin on the domain - just a regular user - and want to avoid installing the Active Directory Libraries if possible (since I would like to share the script with other colleagues also - need minimum pre-reqs)

monojohnny
  • 155
  • 1
  • 6
  • Got marked 'off-topic' ? - I guess it might be a better fit for 'stackoverflow' (general) or 'super user' (maybe?). (Rhetorical question / note to self) – monojohnny Dec 04 '20 at 16:34

1 Answers1

1

Assuming the User Account property DisplayName is "Firstname Lastname" then the following will do what you're looking for

$name = "John Doe"
$search = [adsisearcher]"(&(objectCategory=person)(objectClass=User)(displayname=$name))"
$search.FindAll() | Select-Object -ExpandProperty Properties

Note the userid is known as samAccountName

jfrmilner
  • 406
  • 2
  • 6
  • Brilliant - works a treat. – monojohnny Dec 01 '20 at 12:07
  • For others reading this: I would suggest examining your own user using this Post : https://serverfault.com/questions/582696/retrieve-current-domain-users-full-name , then use this function to verify the reverse-lookup. – monojohnny Dec 01 '20 at 12:08
  • My Powershell Kung Fu is weak....what's the most concise way of getting *just* the 'samaccountname' back from that list..(I should ask a new question really - but worth an ask....) – monojohnny Dec 01 '20 at 12:14
  • Found this: https://jamesmccaffrey.wordpress.com/2008/10/16/searching-active-directory-with-powershell/ Which suggested the idea of holding the result in a var, which you can de-reference using dot-notation: $results=$search.FindAll() $results.properties.samaccountname – monojohnny Dec 01 '20 at 12:26
  • And actually - for my specific example - the 'FindOne()' method is more appropriate. (rather than FindAll()) – monojohnny Dec 01 '20 at 12:28