Powershell – How to get properties for multiple users using Get-ADUser

active-directorypowershell

I was trying to get info on multiple users using Get-ADUser but it doesn't like it. Can someone provide a tip on how to do this? Many Thanks

Get-ADUser -Identity jdoe, jsmith, adoe, bdoe, cdoe -Properties  CN, SamAccountName ,passwordlastset, passwordneverexpires | select  SamAccountName, passwordlastset, passwordneverexpires 

Best Answer

As indicated by the cmdlet help, Identity does not accept an array of input:

Get-ADUser [-Identity] <ADUser> [-AuthType {Negotiate | Basic}] [-Credential <pscredential>]
   [-Partition <string>] [-Properties <string[]>] [-Server <string>]  [<CommonParameters>]

So, you can loop over an array of input:

'jdoe','jsmith','adoe','bdoe','cdoe' | ForEach-Object { 
     Get-AdUser -Identity $_ -Properties PasswordLastSet,PasswordNeverExpires}

Or you can run against an entire OU:

Get-AdUser -Filter * -SearchBase "OU=Users,DC=ad,DC=domain,DC=com" -SearchScope Subtree

Or construct an appropriate LDAPFilter

Get-AdUser -LDAPFilter '(|(sAMAccountName=*doe)(sAMAccountName=jsmith))'

Or any number of other ways. But your best bet is always the help. If you don't have it downloaded, there's always:

 Get-Help Get-AdUser -Online

You should be able to find examples, if not, these should give you an idea of where to start: https://docs.microsoft.com/en-us/powershell/module/activedirectory/get-aduser?view=winserver2012-ps&redirectedfrom=MSDN#examples