1

I got a list of 150+ users and I want to know which group they have membership for? I just started using PS. I can query for 1 user, but not for a list of users. Would like to know exact command??? I got :

(get-aduser -identity "username" -properties memberof |select-object memberof).memberof > c:\temp\ss.csv
CB.
  • 58,865
  • 9
  • 159
  • 159
Svi
  • 11
  • 1
  • 1
  • 2

1 Answers1

1

Read your user list into an array and check if your AD users are contained in that array:

$userlist = Get-Content 'C:\your\userlist.txt'

Get-ADUser -Filter '*' -Properties memberof | Where-Object {
  $userlist -contains $_.SamAccountName
} | ForEach-Object {
  $username = $_
  $groups = $_ | Select-Object -Expand memberof |
            ForEach-Object { (Get-ADGroup $_).Name }
  "{0}: {1}" -f $username, ($groups -join ', ')
} | Out-File 'c:\temp\ss.csv'

Replace SamAccountName as appropriate if the user list doesn't contain the account names of the users.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Sorry to ask, we save this script as .vbs? – Svi Nov 16 '14 at 23:06
  • Hi Ansgar, your script is returning error. Can you check n post it again? Thx. – Svi Nov 17 '14 at 05:04
  • @Svi It's PowerShell, not VBScript, so it should be saved as `.ps1`. And the code works just fine. If you're getting an error, please update your question with the code you used and the error it produced. – Ansgar Wiechers Nov 17 '14 at 08:04
  • Hi, my userlist.txt file structure is like: accountname zachem adelto – Svi Nov 19 '14 at 01:28
  • my script is saved as grpname.ps1 and code in it is : $userlist = Get-Content 'C:\temp\del\userlist.txt' Get-ADUser -Filter 'accountname' -Properties memberof | ? { $userlist -contains $_.AccountName } | select -Expand memberof > 'c:\temp\del\ss.csv' it's not giving error, but not giving any output too ? – Svi Nov 19 '14 at 01:34
  • @Svi Please pay attention to the property names. `SamAccountName` is not the same as `AccountName` (which doesn't exist). – Ansgar Wiechers Nov 19 '14 at 15:17
  • Hi, didn't get time to work on this before, ur script worked. thx. It's giving group membership details, however not showing username with it, so it's getting difficult to identify in big list. All, I want it : username: grpname1, grpname2... then in 2nd row: username: grpname1, grpname 2. What changes should I do? – Svi Nov 21 '14 at 00:41
  • Thanks a lot for the script @AnsgarWiechers. Worked like a charm!! – jeevanreddymandali Jan 05 '18 at 03:14