# Gather information of distribution groups and format them to show in HTML

**URL:** <https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782>\
**Category:** PowerShell Help\
**Created:** [July 8, 2016, 10:25am UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782 "2016-07-08T10:25:34Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![vandrey-trindade](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vandrey-trindade/32/198_2.png) [@vandrey-trindade](https://forums.powershell.org/u/vandrey-trindade)\
**Post date:** [July 8, 2016, 10:25am UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/1 "2016-07-08T10:25:34Z")

</div>

Hi,

I’m trying to gather all distribution groups (Office 365) in my organization and then add their members.  
Example of what I need as result:

List: List - 1  
Members: {John Doe, Alan Wake,etc}

List: List - Abc  
Members: {Shakira, Madonna}

Then I should have a table with all my distribution groups and their members to convert it to an HTML file.

What I was trying:

```
$DistributionGroup = Get-DistributionGroup -ResultSize Unlimited | sort DisplayName
foreach ($DG in $DistributionGroup){
$Results += [PSCustomObject]@{
   DG = $DG.DisplayName
   Members = $Members | ForEach-Object{
      if ($_.RecipientType -like "*MailContact*"){
         $_.Name
         } else {
            $_.DisplayName
            }
         }
      }
   }
```

Obviously it isn’t working =/  
This is the first step… Then I’ll try to convert it to HTML =p

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [July 8, 2016, 11:29am UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/2 "2016-07-08T11:29:38Z")

</div>

$members doesn’t reference anything, we need the full script. You’ll have to show us where you are joining the members into a single string. The object only holds one property. Try not scripting inside the psobject, it can be confusing for beginners.

+= is annoying 🙂

$results = foreach ($DG in $DistributionGroup){  
[PSCustomObject]@{…}}

---

<div class="post-metadata">

**Author:** ![vandrey-trindade](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vandrey-trindade/32/198_2.png) [@vandrey-trindade](https://forums.powershell.org/u/vandrey-trindade)\
**Post date:** [July 8, 2016, 11:41am UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/3 "2016-07-08T11:41:29Z")

</div>

Sorry,

Forgot that line =/

```
$DistributionGroup = Get-DistributionGroup -ResultSize Unlimited | sort DisplayName
foreach ($DG in $DistributionGroup){
$Members = Get-DistributionGroupMember $DG
   $Results = [PSCustomObject]@{
      DG = $DG.DisplayName
      Members = $Members | ForEach-Object{
         if ($_.RecipientType -like "*MailContact*"){
            $_.Name
            } else {
               $_.DisplayName
               }
         }
      }
   }
```

Using that with only one DG (removing the first two lines of the script and setting $DG), when I type $Results.DGS and $Results.Members it shows what I want… Want to add them all in a single variable to be able to export it…

```
$DG = Get-DistributionGroup support
$Members = Get-DistributionGroupMember $DG.DisplayName
   $Results = [PSCustomObject]@{
      DG = $DG.DisplayName
      Members = $Members | ForEach-Object{
         if ($_.RecipientType -like "*MailContact*"){
            $_.Name
            } else {
               $_.DisplayName
               }
         }
      }
```

[Image with the results](https://postimg.org/image/6r6knk2g1/)

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [July 8, 2016, 12:19pm UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/4 "2016-07-08T12:19:05Z")

</div>

```
#when troubleshooting add this | select -first 1

$DistributionGroup = Get-DistributionGroup -ResultSize Unlimited | sort DisplayName | select -first 1

foreach ($DG in $DistributionGroup) {
	
	$Members = Get-DistributionGroupMember $DG | ForEach-Object{
		if ($_.RecipientType -like "*MailContact*") { $_.Name } else { $_.DisplayName }
	}
	
	$string = $members -join '; '
		
		$Results = [PSCustomObject]@{
			DG = $DG.DisplayName
			Members = $string
	}
	
}
```

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [July 8, 2016, 12:24pm UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/5 "2016-07-08T12:24:27Z")

</div>

You could even place the $results = in front of the first foreach.

---

<div class="post-metadata">

**Author:** ![vandrey-trindade](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vandrey-trindade/32/198_2.png) [@vandrey-trindade](https://forums.powershell.org/u/vandrey-trindade)\
**Post date:** [July 8, 2016, 12:24pm UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/6 "2016-07-08T12:24:59Z")

</div>

Dan Potter,

Thanks! But still it doesn’t work for more than 1 DG.  
It only keep the “last” DG processed…  
How to keep them all inside $Results?

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [July 8, 2016, 12:33pm UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/7 "2016-07-08T12:33:00Z")

</div>

Making me go look for an exchange server costs money 🙂

```
$DistributionGroup = Get-DistributionGroup -ResultSize Unlimited | sort DisplayName | select -first 1

$Results = foreach ($DG in $DistributionGroup) {
	
	$Members = Get-DistributionGroupMember $DG | ForEach-Object{
		if ($_.RecipientType -like "*MailContact*") { $_.Name } else { $_.DisplayName }
	}
	
	$string = $members -join '; '
		
		 [PSCustomObject]@{
			DG = $DG.DisplayName
			Members = $string
	}

	
}

$results
```

---

<div class="post-metadata">

**Author:** ![vandrey-trindade](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vandrey-trindade/32/198_2.png) [@vandrey-trindade](https://forums.powershell.org/u/vandrey-trindade)\
**Post date:** [July 8, 2016, 12:39pm UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/8 "2016-07-08T12:39:50Z")

</div>

=p

Worked perfectly! Many thanks!

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [July 8, 2016, 12:52pm UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/9 "2016-07-08T12:52:39Z")

</div>

NP!

btw, I was wrong in the |select -first 1 The warning for exchange management shell threw me off course. Seems there has been no development for it since 2007!

|select -first 1 works great in plain PS when enumerating a lot of objects but slower than a prius in EMS.

---

<div class="post-metadata">

**Author:** ![vandrey-trindade](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vandrey-trindade/32/198_2.png) [@vandrey-trindade](https://forums.powershell.org/u/vandrey-trindade)\
**Post date:** [July 8, 2016, 12:54pm UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/10 "2016-07-08T12:54:47Z")

</div>

I’m using Exchange Online… It worked for me =p

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [July 8, 2016, 1:04pm UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/11 "2016-07-08T13:04:56Z")

</div>

I could have done that as well but I’m too lazy to save the connection string =D Maybe next year when I use it again.

---

<div class="post-metadata">

**Author:** ![vandrey-trindade](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vandrey-trindade/32/198_2.png) [@vandrey-trindade](https://forums.powershell.org/u/vandrey-trindade)\
**Post date:** [July 8, 2016, 1:17pm UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/12 "2016-07-08T13:17:37Z")

</div>

Lol

Ok… That was the first part =p

Now I’m trying to send an e-mail with that info, using this:

```
$Body = ""

foreach ($item in $Results) {
  $Body+="$($item.DG)"
  $Body+= $item.Members |
  Convertto-Html -Fragment -As Table
}

Send-MailMessage -SmtpServer "server" -From "teste@mail.com" -To "mymail@organization.com" -Subject "DGs" -Body $Body -BodyAsHtml -Encoding UTF8
```

It returns:

DG Name  
\*  
109

¬¬

If I remove the “| Convertto-Html -Fragment -As Table” it shows the names like you formatted them to me “name; name2; name 3; name 4”  
In a DG with a lot of members looks awful to read it…  
Tried using “$Body+= $item.Members.Split(”;“)” but didn’t worked too…

---

<div class="post-metadata">

**Author:** ![vandrey-trindade](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vandrey-trindade/32/198_2.png) [@vandrey-trindade](https://forums.powershell.org/u/vandrey-trindade)\
**Post date:** [July 8, 2016, 1:35pm UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/13 "2016-07-08T13:35:24Z")

</div>

Hi again,

That’s was the “first” part =p  
Now I’m trying to send it by mail using this:

```
$Body = ""
foreach ($item in $Results) {
  $Body+="$($item.DG)"
  $Body+= $item.Members |
  Convertto-Html -Fragment -As Table
}

Send-MailMessage -SmtpServer "server" -From "abcdatmail.com" -To "abcdeatmail.com" -Subject "DGs -Body $Body -BodyAsHtml
```

But it returns something like this:

DG name  
\*  
109

¬¬

When I remove the “| Convertto-Html -Fragment -As Table” it returns the DG name and all the members like “name; name1; name2; name3”. It looks awful to read when a DG has a lot of members  
Tried to use “$Body+= $item.Members.Split(”;“)” but didn’t worked too…

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [July 8, 2016, 1:46pm UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/14 "2016-07-08T13:46:53Z")

</div>

Using Select -First 1 doesn’t really buy you a lot using it like that. You are still getting ALL of the Distribution Groups and then selecting the first 1. You should always filter as far left as possible, so using the -Filter or -Identity to select a smaller resultset to test your code. I use Select -First, but I’ll normally pull a large resultset once, keep it in memory and then remark it out, so something like this:

```
#$DistributionGroup = Get-DistributionGroup -ResultSize Unlimited | sort DisplayName
$DistributionGroup = $DistributionGroup | Select -First 3
```

Also, tested this as another possible solution:

```
$DistributionGroup = Get-DistributionGroup -ResultSize Unlimited | sort DisplayName | select -first 1

$results = foreach ($DG in $DistributionGroup) {
	Get-DistributionGroupMember $DG.Identity | Select Name, DisplayName, Id, @{Name="DistributionGroup";Expression={$DG}}
}

$results
```

Edit: To send as HTML, try something like this…

```
$resultsHTML = $results | ConvertTo-HTML -As Table

$mailParams = @{
    To = "abcdeatmail.com"
    From = "abcdatmail.com"
    Subject = "Report: Distribution Group"
    Body = $resultsHTML
    BodyAsHtml = $true
    SmtpServer = "server"
}

Send-MailMessage @mailParams
```

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [July 8, 2016, 2:48pm UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/15 "2016-07-08T14:48:23Z")

</div>

Right, I merely use it as a troubleshooting option instead of rewriting the query twice.

In the shell I would do $DistributionGroup = $DistributionGroup | select -first 1 then copy and paste the foreach. Just a quick time saver.

---

<div class="post-metadata">

**Author:** ![vandrey-trindade](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vandrey-trindade/32/198_2.png) [@vandrey-trindade](https://forums.powershell.org/u/vandrey-trindade)\
**Post date:** [July 11, 2016, 6:14am UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/16 "2016-07-11T06:14:47Z")

</div>

Hi again,

That’s was the “first” part =p  
Now I’m trying to send it by mail using this:

```
$Body = ""
foreach ($item in $Results) {
  $Body+="$($item.DG)"
  $Body+= $item.Members |
  Convertto-Html -Fragment -As Table
}

Send-MailMessage -SmtpServer "server" -From "abcdatmail.com" -To "abcdeatmail.com" -Subject "DGs -Body $Body -BodyAsHtml
```

But it returns something like this:

DG name  
\*  
109

¬¬

When I remove the “| Convertto-Html -Fragment -As Table” it returns the DG name and all the members like “name; name1; name2; name3”. It looks awful to read when a DG has a lot of members  
Tried to use “$Body+= $item.Members.Split(”;“)” but didn’t worked too…

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [July 11, 2016, 7:31am UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/17 "2016-07-11T07:31:11Z")

</div>

$body = $results |converto-html |out-string

---

<div class="post-metadata">

**Author:** ![vandrey-trindade](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/vandrey-trindade/32/198_2.png) [@vandrey-trindade](https://forums.powershell.org/u/vandrey-trindade)\
**Post date:** [July 11, 2016, 7:49am UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/18 "2016-07-11T07:49:54Z")

</div>

Hi Dan,

It sends the e-mail just like we see the $Results on PowerShell.  
I wanted to change the mode it is displayed.

```
foreach ($item in $Results) {
  $Body+="$($item.DG)"
  $Body+= $item.Members
}
```

In this way I get the DG DisplayName in bold and bigger size than regular.  
Then I get the members, but they are all in on line. I wanted one line for each member…

Images of the results: [http://postimg.org/gallery/lwvl77du/](http://postimg.org/gallery/lwvl77du/)

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [July 11, 2016, 8:02am UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/19 "2016-07-11T08:02:16Z")

</div>

Try this and see what you get.

```
$a = ""
		$a = $a + "BODY{background-color:white;FONT-SIZE: 8pt; FONT-FAMILY: verdana}"
		$a = $a + "TABLE{width: 975px;border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
		$a = $a + "TH{width: 975px;border-width: 1px;padding: 3px;border-style: solid;border-color: black;color: Green;background-color:#CCCCCC}"
		$a = $a + "TD{width: 975px;border-width: 1px;padding: 3px;border-style: solid;border-color: black;background-color:white}"
		$a = $a + ""

$results |select -first 1 | ConvertTo-Html -head $a -body "$item.DG " | out-string
```

---

<div class="post-metadata">

**Author:** ![dan-potter](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/dan-potter/32/138_2.png) [@dan-potter](https://forums.powershell.org/u/dan-potter)\
**Post date:** [July 11, 2016, 8:05am UTC](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782/20 "2016-07-11T08:05:33Z")

</div>

Well, this forum strips out html code so… look up formatting html headers powershell.

This part has html breaks after the $item.dg ConvertTo-Html -head $a -body "$item.DG lessthan br greaterthan "

[Next page](https://forums.powershell.org/t/gather-information-of-distribution-groups-and-format-them-to-show-in-html/6782.md?page=2)
