# finding members of a group and printing out name and email

**URL:** <https://forums.powershell.org/t/finding-members-of-a-group-and-printing-out-name-and-email/7877>\
**Category:** PowerShell Help\
**Created:** [January 18, 2017, 5:24pm UTC](https://forums.powershell.org/t/finding-members-of-a-group-and-printing-out-name-and-email/7877 "2017-01-18T17:24:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![brian-catlin](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@brian-catlin](https://forums.powershell.org/u/brian-catlin)\
**Post date:** [January 18, 2017, 5:24pm UTC](https://forums.powershell.org/t/finding-members-of-a-group-and-printing-out-name-and-email/7877/1 "2017-01-18T17:24:56Z")

</div>

Hi,

I needed a power shell script that would pull all the members out of a group and pipe them to a CSV. I could get it to work if I used FT (format command to the screen but I remember we cannot pipe formatted stuff to csv)

I figured out how to do this but it was kind of corny, and I don’t think it was done properly. I don’t really study powershell anymore as the last year or so has been spent getting my CCNA and now working on my CCNP. I love Power Shell and use to study it but I never got to use much of it at work which is why I pushed hard in the network direction.

Anyway my code below worked but but put a bunch of crud into the csv and I cheated by then saving it as a Excel workbook and just deleting out the columns I did not want.

```
Get-ADGroupMember "D_GroupName" | Get-ADUser -Properties name,emailaddress | export-csv -NoTypeInformation c:\scripts\D_GroupName.csv
```

What would be a much cleaner and better way to do this where my outputted information was only the properties that I wished instead of all the generic properties?

---

<div class="post-metadata">

**Author:** ![gazza](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/gazza/32/130_2.png) [@gazza](https://forums.powershell.org/u/gazza)\
**Post date:** [January 18, 2017, 7:22pm UTC](https://forums.powershell.org/t/finding-members-of-a-group-and-printing-out-name-and-email/7877/2 "2017-01-18T19:22:27Z")

</div>

If I understand correctly, you just want the Name,Email in the CSV?  
You can pipe to select.

```
Get-ADGroupMember "D_GroupName" | Get-ADUser -Properties name,emailaddress | Select name,emailaddress | export-csv -NoTypeInformation c:\scripts\D_GroupName.csv
```

I personally don’t like piping everything in a script. I would write it like this, but i’m sure someone else could probably write it cleaner.

```
$dl = "D_GroupName"

$members = Get-ADGroupMember $dl
foreach ($member in $members){
Get-ADUser -Filter {DistinguishedName -like $member} -Properties name,mail | Select name,mail
}
```

---

<div class="post-metadata">

**Author:** ![brian-catlin](https://avatars.discourse-cdn.com/v4/letter/b/41988e/32.png) [@brian-catlin](https://forums.powershell.org/u/brian-catlin)\
**Post date:** [January 19, 2017, 8:59am UTC](https://forums.powershell.org/t/finding-members-of-a-group-and-printing-out-name-and-email/7877/3 "2017-01-19T08:59:32Z")

</div>

I got the first one you gave me to work perfectly the second one I was looking at but it gave me an error:

```
$dl = "D_GroupName"

$members = Get-ADGroupMember $dl
foreach ($member in $members){
Get-ADUser -Filter {DistinguishedName -like $member} -Properties name,mail | Select name,mail
}

Get-ADUser : Invalid type 'Microsoft.ActiveDirectory.Management.ADPrincipal'.
Parameter name: distinguishedName
At line:5 char:1
+ Get-ADUser -Filter {DistinguishedName -like $member} -Properties name,mail | Sel ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
```

I was thinking maybe the word DistinguishedName was spelled incorrectly but I looked a user up under Attribute Editor in the Active directory and that looks to be the correct Attribute name and spelling.

I found this post that said something about a bug its very close to the same error but not sure if that is related or not but figured I would share the website.

[https://blogs.technet.microsoft.com/pstips/2014/12/02/get-aduser-one-or-more-properties-are-invalid-parameter-name-msds-assignedauthnpolicy/](https://blogs.technet.microsoft.com/pstips/2014/12/02/get-aduser-one-or-more-properties-are-invalid-parameter-name-msds-assignedauthnpolicy/)

---

<div class="post-metadata">

**Author:** ![aapeli-hietikko](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aapeli-hietikko](https://forums.powershell.org/u/aapeli-hietikko)\
**Post date:** [January 27, 2017, 1:41pm UTC](https://forums.powershell.org/t/finding-members-of-a-group-and-printing-out-name-and-email/7877/4 "2017-01-27T13:41:06Z")

</div>

un-tested as I don’t have domain to play with, but this little tweak should do the trick. I’d say that using  
$member.DistinguishedName would not work inside the filter as ad module is a bummer.

```
$dl = "D_GroupName"

$members = Get-ADGroupMember $dl
foreach ($member in $members){
$dn = $member.DistinguishedName
Get-ADUser -Filter {DistinguishedName -like $dn} -Properties name,mail | Select name,mail
}
```

It could also work like this:

```
$dl = "D_GroupName"

$members = Get-ADGroupMember $dl
foreach ($member in $members.DistinguishedName){
Get-ADUser -Filter {DistinguishedName -like $member} -Properties name,mail | Select name,mail
}
```

---

<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:39pm UTC](https://forums.powershell.org/t/finding-members-of-a-group-and-printing-out-name-and-email/7877/5 "2024-05-16T20:39:33Z")

</div>


