Generate list of all members of specific AD groups with properties

Welcome Forums General PowerShell Q&A Generate list of all members of specific AD groups with properties

  • This topic has 3 replies, 4 voices, and was last updated 1 year ago by
    Senior Moderator
    .
Viewing 3 reply threads
  • Author
    Posts
    • #163974
      Participant
      Topics: 2
      Replies: 2
      Points: 32
      Rank: Member

      Hello Guys,

       

      I’m working on a script which will be used to audit members of all Ad groups with a specific string in the name. I want to generate a table with the group name and all members underneath – which will include their full name, title, department etc.

       

      I have got most of this working but i know need to output the results to a spreadsheet and when i try to pipe format table into export csv, i get junk (i assume export csv doesn’t support input from format table).  I also cannot skip the format table function as i need it to format the information. Can anyone advise on how i could save the output in a way which will look the same as the console output when the script is run?

      I’ll also add that i am new to powershell so i may be missing something obvious. I’ve also removed sensitive data such as ou path and search string.

      Here is the code:

       

       

      $Groups = Get-ADGroup -SearchBase "OU=x,DC=x,DC=x,DC=x" -Filter "name -like '*search-string*'"
      
      
      Foreach($Group in $Groups) {
          echo $Group
          Get-ADGroupMember -Identity $Group | Get-ADObject -Properties Name, Title, Department| Format-Table Name, Title, Department 
      
      }
      
      

      Thanks

      A

    • #164007
      Participant
      Topics: 2
      Replies: 31
      Points: 71
      Rank: Member

      You could change it just slightly. The select object will create a new object with only those properties so your export should be cleaner.

      Get-ADGroupMember -Identity $Group | Get-ADObject -Properties Name, Title, Department| Select-Object -Property Name, Title, Department | Export-csv -path somepath.csv
      
      
    • #164010
      Participant
      Topics: 1
      Replies: 7
      Points: 12
      Rank: Member

      I think you can take out the “format-table” & put in “Select” or “Select-Object” instead when exporting to CSV.

      Then, add the “| Export-CSV FILENAME.CSV -NoTypeInformation” to the end of the script to create the CSV file.

      Here’s one I wrote. It’s a bit different than yours.  I figured how to insert the group name into the CSV file name to get a unique CSV for each group.  The only drawback is that you would need to enter the exact group name for my script to work, although you could probably change that part if you don’t like it.

      Import-Module ActiveDirectory
      $Group = Read-Host -Prompt ‘Enter the group name’
      Get-ADGroupMember -Identity $Group | Get-ADUser -properties mail, title, department| Select Name,samAccountName,Mail,Title, Department `
      | sort Name | Export-Csv GroupMembers_$($Group).csv -NoTypeInformation
      
      

       

      Alicia

    • #164013
      Senior Moderator
      Topics: 9
      Replies: 1236
      Points: 4,443
      Helping Hand
      Rank: Community Hero

      Format-Table really kills the object, its meant only for console outputs. Same is the behavior of all Format-* cmdlets.
      Export-Csv needs object and follow the Select-Object option mentioned by others above.

Viewing 3 reply threads
  • The topic ‘Generate list of all members of specific AD groups with properties’ is closed to new replies.