Exchange PS Script to Export the Unused Distribution List in Exchange Server as .CSV?

Ask your Windows PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

Exchange PS Script to Export the Unused Distribution List in Exchange Server as .CSV?

Post by ITEngineer » Wed Aug 22, 2018 5:46 am

Hi,

Using PowerShell and Exchange 2013 remoting from my laptop,

I need some help in fixing the below script to export the least used Distribution Group:

Code: Select all

$hts = Get-TransportService
$dls = Get-DistributionGroup –ResultSize Unlimited
$hts | ForEach-Object {
	Get-MessageTrackingLog -ResultSize Unlimited -Server $_.Name -EventId Expand -Start (get-date).toshortdatestring() |
	ForEach-Object { [int]$dls[$_.RelatedRecipientAddress] += 1 }
}

$dls | Export-Csv -Path C:\EE\Result.csv -NoTypeInformation
The goal is to be able to Export the unused or least used Distribution List from the complete list of DL in my Address Book as .CSV
/* IT Engineer */

User avatar
dwight.brookland
Posts: 15
Joined: Sun Mar 03, 2013 12:43 pm

Re: Exchange PS Script to Export the Unused Distribution List in Exchange Server as .CSV?

Post by dwight.brookland » Wed Aug 22, 2018 9:13 am

Why not pull a list of DL's from Exchange and then push that through an Get-ADGroupMember cmdlet using the ().count and assigning to variable. Then pushing that out to an out-gridview of those that are empty. So something like

$emptDLs = @()
$DLs = Get-DistributionGroup
$DLs | Foreach {
$count = (Get-ADGroupMember -identity $_.Name).Count
If ($count -gt "0") {
Write-Host "DL $_.DisplayName is empty. Adding to list"
$emptyDLs = $emtpDLs + $_ (Or something like that. I always mix up the operators and have to look them up.)
}
$emptyDLs | Out-Gridview or Out-File with an append

User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

Re: Exchange PS Script to Export the Unused Distribution List in Exchange Server as .CSV?

Post by ITEngineer » Wed Aug 22, 2018 4:22 pm

Dwight,

Thanks for the reply. However, the script that you've suggested somehow is not working as it should:

Code: Select all

$emptDLs = @()
$DLs = Get-DistributionGroup 
$DLs | Foreach {
$count = (Get-ADGroupMember -identity $_.Name).Count 
	If ($count -gt "0") {
		Write-Host "DL $_.DisplayName is empty. Adding to list"
		$emptyDLs = $emtpDLs + $_
	}
}

$emptyDLs | Out-Gridview 
It lists all Distribution List eventhough it has one member in it?
/* IT Engineer */

User avatar
jvierra
Posts: 13470
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Exchange PS Script to Export the Unused Distribution List in Exchange Server as .CSV?

Post by jvierra » Wed Aug 22, 2018 4:42 pm

"Count" is an integer and not a string. Always remember that string numbers cannot normally be compare correctly.

User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

Re: Exchange PS Script to Export the Unused Distribution List in Exchange Server as .CSV?

Post by ITEngineer » Wed Aug 22, 2018 5:10 pm

jvierra wrote:
Wed Aug 22, 2018 4:42 pm
"Count" is an integer and not a string. Always remember that string numbers cannot normally be compare correctly.
Hm.. no wonder it doesn't work.

however, I've tried to modify the script to be like the following:

Code: Select all

$dist = (Get-DistributionGroup -Filter {Name -like "*IT*"} –ResultSize Unlimited | Where-Object { (Get-DistributionGroupMember –Identity $_.Name –ResultSize Unlimited).Count -le 1});
Foreach ($d in $dist) {
 $d | Add-Member -Force -NotePropertyName Count -NotePropertyValue (Get-DistributionGroupMember -Identity $d.name).Count;
 $d | Add-Member -Force -NotePropertyName MemberName -NotePropertyValue (Get-DistributionGroupMember -Identity $d.name).EmailAddresses;
 $d | Select-Object Name, Count, @{Name="EmailAddresses";Expression={$_.emailaddresses}} | Export-CSV "C:\Result-IT.csv" -append -NoTypeInformation
}
Somehow it is partially working.

Not working: display an additional column for one DL member?
/* IT Engineer */

User avatar
jvierra
Posts: 13470
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Exchange PS Script to Export the Unused Distribution List in Exchange Server as .CSV?

Post by jvierra » Wed Aug 22, 2018 6:41 pm

Your issue is very muddy. If you want empty groups then this is the method:

Code: Select all

$dist = (Get-DistributionGroup -Filter {Name -like "*IT*"} –ResultSize Unlimited | 
    Where-Object { 
        (Get-DistributionGroupMember –Identity $_.Name –ResultSize Unlimited).Count -eq 0}
)

User avatar
ITEngineer
Posts: 138
Joined: Wed Oct 12, 2011 10:52 am

Re: Exchange PS Script to Export the Unused Distribution List in Exchange Server as .CSV?

Post by ITEngineer » Wed Aug 22, 2018 10:27 pm

jvierra wrote:
Wed Aug 22, 2018 6:41 pm
Your issue is very muddy. If you want empty groups then this is the method:

[/code]
$dist = (Get-DistributionGroup -Filter {Name -like "*IT*"} –ResultSize Unlimited |
Where-Object {
(Get-DistributionGroupMember –Identity $_.Name –ResultSize Unlimited).Count -eq 0}
)[/code]
Yes, that does make sense.
Thanks for the assistance Mr. Vierra :)

it works great.
/* IT Engineer */

Locked