Svi
Svi

Reputation: 11

How to get multiple users membership groups from AD using Powershell script?

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

Upvotes: 1

Views: 12993

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200543

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.

Upvotes: 1

Related Questions