1

I am trying to get list of AD groups a user is part of and I have a script that works. But I need to show it in a different manner. Currently the script shows a record in one line with username and then multiple group names in the next cell delimited by a semi-colon.

I would like it in a way that all groups are in each seperate row and the username gets repeated.

Example:

DavidChow - Server Admin Group

DavidChow - Desktop Admin Group

DavidChow - Azure Admin Group

NinaChow - Desktop User Group

This is my script:

        $UnameList= Import-Csv C:\temp\users_full_list.csv
ForEach ($username in $UnameList){
$groups=Get-ADPrincipalGroupMembership -Identity $username.Identity | select -expand name
$data = [PSCustomObject]@{
          samaccountname = $username.Identity
          count = $groups.count
          memberOf = ($groups -join ';')}
 Write-Output $data  | Export-Csv C:\Temp\users_full_list_memberships.csv -Delimiter ";" -NoTypeInformation -Append
         }
1
  • Did my answer help? Commented Nov 6, 2020 at 11:47

1 Answer 1

1

You could create a "data" object for each group of each user, and then export all to CSV at the end:

Import-Csv C:\temp\users_full_list.csv | foreach {
    $identity = $_.Identity
    Get-ADPrincipalGroupMembership -Identity $identity | foreach {
        [PSCustomObject]@{
              SamAccountName = $identity
              GroupName = $_.Name
        }
    }
} | Export-Csv C:\Temp\users_full_list_memberships.csv -Delimiter ";" -NoTypeInformation
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.