# Generate list of all members of specific AD groups with properties

**URL:** <https://forums.powershell.org/t/generate-list-of-all-members-of-specific-ad-groups-with-properties/12807>\
**Category:** PowerShell Help\
**Created:** [July 3, 2019, 10:49am UTC](https://forums.powershell.org/t/generate-list-of-all-members-of-specific-ad-groups-with-properties/12807 "2019-07-03T10:49:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![andyf](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@andyf](https://forums.powershell.org/u/andyf)\
**Post date:** [July 3, 2019, 10:49am UTC](https://forums.powershell.org/t/generate-list-of-all-members-of-specific-ad-groups-with-properties/12807/1 "2019-07-03T10:49:42Z")

</div>

Hello Guys,

&nbsp;

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.

&nbsp;

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:

&nbsp;

&nbsp;

```
$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

---

<div class="post-metadata">

**Author:** ![willprather](https://avatars.discourse-cdn.com/v4/letter/w/41988e/32.png) [@willprather](https://forums.powershell.org/u/willprather)\
**Post date:** [July 3, 2019, 10:56am UTC](https://forums.powershell.org/t/generate-list-of-all-members-of-specific-ad-groups-with-properties/12807/2 "2019-07-03T10:56:31Z")

</div>

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

[pre]

Get-ADGroupMember -Identity $Group | Get-ADObject -Properties Name, Title, Department| Select-Object -Property Name, Title, Department | Export-csv -path somepath.csv

[/pre]

---

<div class="post-metadata">

**Author:** ![spike99](https://avatars.discourse-cdn.com/v4/letter/s/edb3f5/32.png) [@spike99](https://forums.powershell.org/u/spike99)\
**Post date:** [July 3, 2019, 11:19am UTC](https://forums.powershell.org/t/generate-list-of-all-members-of-specific-ad-groups-with-properties/12807/3 "2019-07-03T11:19:11Z")

</div>

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.

[pre]

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

[/pre]

&nbsp;

Alicia

---

<div class="post-metadata">

**Author:** ![kvprasoon](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kvprasoon/32/4449_2.png) [@kvprasoon](https://forums.powershell.org/u/kvprasoon)\
**Post date:** [July 3, 2019, 11:36am UTC](https://forums.powershell.org/t/generate-list-of-all-members-of-specific-ad-groups-with-properties/12807/4 "2019-07-03T11:36:38Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:32pm UTC](https://forums.powershell.org/t/generate-list-of-all-members-of-specific-ad-groups-with-properties/12807/5 "2024-05-16T20:32:02Z")

</div>

Closed on May 16, 2024, 8:32pm UTC


