locked
*Tricky*: Get members of specific AD groups w/ special formatting showing which groups it found each user in.. RRS feed

  • General discussion

  • Hi All,

    I'm new to the forum & Powershell but I wanted to give this a shot as I've been struggling for a while on it:

    What I'm looking to do is to scrub AD for members of 5-10 specific AD groups (Easy enough)

    * HOWEVER *, the tricky part is: I'm looking for sort-of a different format for how it shows what groups each user is in..

    Lets say I tell powershell to show me the users in Group1,Group2,Group3,Group4 & Group5..Here's how I want the output to look:

    AccountName FirstName LastName GroupMembership
    dmcsprinkles Donut McSprinkles Group2,Group3,Group5
    hburger Hank Burger Group1,Group2
    nspagetters Noodles Spagetters Group4



    Or a column for each group would be fine too.

    Everything I've been able to script myself or seen in forums gives me back a CSV with the same user listed multiple times (per each group it found them in - which won't work for the project I'm on right now.

    Any help would be much appreciated!!

    Thanks!



    • Changed type Bill_Stewart Thursday, January 25, 2018 10:30 PM
    • Moved by Bill_Stewart Thursday, January 25, 2018 10:30 PM This is not "scripts on demand"
    Wednesday, November 15, 2017 11:04 PM

All replies

  • Here's where I started - However, this gives me the same user listed multiple times per each group it found them in..
    ----------------------------------------------------------------------
    import-module ActiveDirectory

    $Groups = Get-Content C:\Temp\GroupList.txt

    $Users = ForEach ($group in $Groups) {
    Get-ADUser -Filter * -Properties memberof | Where {$_.memberof -like "*$group*"} | %{
      New-Object PSObject -Property @{
          AccountName = $_.sAMAccountName
          FirstName = $_.givenName
          LastName = $_.SurName
          Enabled = $_.Enabled
          Description = $_.Description
          GroupMembership = (($_.memberof | Get-ADGroup | Where {$_.name -like "*$group*"} | select name -expandproperty name) -join ",")}
          }} 
          
    $Users | Select AccountName, FirstName, LastName,GroupMembership,Enabled, Description | sort-object -property AccountName | Export-Csv -Path C:\Temp\Test.csv -NoTypeInformation

    ii C:\Temp\Test.csv
    Wednesday, November 15, 2017 11:05 PM
  • However, this gives me the same user listed multiple times per each group it found them in..
    Select-Object -Unique

    Best regards
    (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Thursday, November 16, 2017 2:08 AM
    Thursday, November 16, 2017 2:07 AM
  • My usual recommendation is to dump the raw data to CSV (one group name per line) and then import into a spreadsheet and do a cross-tabulation (Excel calls this a "pivot table" I believe).

    -- Bill Stewart [Bill_Stewart]

    Thursday, November 16, 2017 4:36 PM