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

Ask your 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.
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post by ITEngineer »

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: 17
Last visit: Tue Mar 26, 2024 11:24 am

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

Post by dwight.brookland »

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: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post by ITEngineer »

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 */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

"Count" is an integer and not a string. Always remember that string numbers cannot normally be compare correctly.
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post by ITEngineer »

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 */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

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: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post by ITEngineer »

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 */
This topic is 5 years and 7 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked