Powershell - Removing Unused Distribution Lists In The Exchange 2010

We currently have over 900 Distribution Lists (DLs) within our mail environment – many of which we have believe to be unused/orphaned.  I have tested the following steps in an effort to automate a task which could be scheduled (every 90 days?) to identify and remove unused DLs in our Exchange 2010 environment.

Steps:
  1. Increase Exchange MessageTrackingLogs from the default of 30 days (1Gb) to 90 days (3Gb)
  2. Export list of ALL DLs
  3. Export list of ALL active DLs based off Exchange Tracking Logs
  4. Compare the results and output the inactive DLs
  5. Hide all unused DLs from the GAL
  6. Delete all unused DLs from Exchange
Notes:
  • Note these are server specific commands and I need to do this on all hub transport servers
  • Increasing the log will not remove the original logs
  • We will have to wait 2 months after setting these logs up
Ref:
Configure Message Tracking (http://technet.microsoft.com/en-us/library/aa997984.aspx)

1. Configuring Exchange MessageTrackingLogs settings
Get-TransportServer | fl name,*messagetracking*

 Get-TransportServer | `
  Set-TransportServer -MessageTrackingLogMaxDirectorySize 3072MB `
  -MessageTrackingLogMaxAge 180.00:00:00
  
 Get-TransportServer | fl name,*messagetracking*
2. Export list of ALL DLs
Get-DistributionGroup -ResultSize Unlimited | `
  Select-Object PrimarySMTPAddress | `
  Sort-Object PrimarySMTPAddress | `
  Export-CSV DL-ALL_Present_Tense.csv -notype
3. Export list of ALL active  DLs based off Exchange Tracking Logs
Get-TransportServer | `
  Get-MessageTrackingLog -EventId Expand -ResultSize Unlimited | `
  Sort-Object RelatedRecipientAddress | `
  Group-Object RelatedRecipientAddress | `
  Sort-Object Name | `
  Select-Object @{label="PrimarySmtpAddress"; `
  expression={$_.Name}}, Count | `
  Export-CSV DL-ACTIVE_Historical.csv –notype
4. Compare the results and output the inactive DLs
$file1 = Import-CSV -Path "DL-ALL_Present_Tense.csv"
 $file2 = Import-CSV -Path "DL-ACTIVE_Historical.csv"
 Compare-Object $file1 $file2 -syncWindow 1000 `
  -Property PrimarySmtpAddress -PassThru | `
  Where-Object{$_.SideIndicator -eq '<='} | `
  Select-Object -Property PrimarySmtpAddress | `
  Export-Csv DL-EXISTING_DLs_Inactive_last_180_days.csv -NoType
5. Compare the results and output the "non-existing" active DLs (documentation purposes only)
$file1 = Import-CSV -Path "DL-ALL_Present_Tense.csv"
 $file2 = Import-CSV -Path "DL-ACTIVE_Historical.csv"
 Compare-Object $file1 $file2 -syncWindow 1000 `
  -Property PrimarySmtpAddress -PassThru | `
  Where-Object{$_.SideIndicator -eq '=>'} | `
  Select-Object -Property PrimarySmtpAddress | `
  Export-Csv DL-NONEXISTING_DLs_Inactive_last_180_days.csv -NoType
6a. Hide all unused DLs from the Global Address List
$a = Get-Date
 $notes = "$a - Hidden from address list due to inactive use."
 $inactiveDL = Import-CSV -Path "DL-EXISTING_DLs_Inactive_for_180_days.csv"
 foreach ($DL in $inactiveDL)
   {
    Set-Group -identity $DL.PrimarySmtpAddress -notes $notes
    Set-DistributionGroup -identity $DL.PrimarySmtpAddress `
     -HiddenFromAddressListsEnabled $true
   }
6b. Delete all unused DLs from the Global Address List
$a = Get-Date
 $notes = "$a - No longer mail enabled due to inactive use."
 $inactiveDL = Import-CSV -Path "DL-EXISTING_DLs_Inactive_for_180_days.csv"
 foreach ($DL in $inactiveDL)
  {
   Set-Group -identity $_.PrimarySmtpAddress -notes $notes
   Disable-DistributionGroup -identity $_.PrimarySmtpAddress -Confirm $false
  }

No comments:

Post a Comment