Getting User Group member of & samacccountname of all users within an ou

I would like the following format(if the group names can be added individually in column b, and samaccountname in column a):

Example:

we have 3 users in ou united, and user's IDs are ABC, XXX, and YYY, could I please have a powershell script to get the following output:

ABC group1

ABC group2

ABC groupx

XXX group1

XXX group2

YYY group1

YYY group2

Basically, I would like the samaccount name repeated in column A for each group that the user has. I was able to write a script that enters all of the groups in column b, but I need them added individually in each row.

If the output could be exported to a CSV that would be appreciated.

Thanks

 

 

 

This is what I am currently working with which outputs the groups in column b all at once separated by a comma instead of in column b individually with the sammaccountname in column a:

 

Import-Module Activedirectory
Get-ADUser -Filter * -Properties samaccountname,memberof -SearchBase "my ou" | % {
  New-Object PSObject -Property @{
UserName = $_.samaccountname
Groups = ($_.memberof | Get-ADGroup | Select -ExpandProperty Name) -join ","
}
} | Select UserName,Groups | Export-Csv C:\temp\report.csv -NTI

Parents
  • Import-Module Activedirectory

    Get-ADUser -Filter * -Properties samaccountname,memberof -SearchBase "my ou" | 

    foreach {

      $sam = $_.samaccountname

      foreach ($group in $_.memberof) {

        New-Object PSObject -Property @{

          UserName = $_.samaccountname;

          Group = ($group -split ",")[0].Substring(3);

        }

      }

    } | Select UserName,Group | Export-Csv C:\temp\report.csv -NTI

Reply Children
No Data