This PowerShell script automates the deletion of distribution lists from Exchange Online or on-premises Exchange using a CSV file as input.
- CSV-based bulk deletion of distribution lists
- Automatic column detection - supports Identity, EmailAddress, DisplayName, etc.
- Comprehensive logging with timestamped log files
- WhatIf mode for testing before actual deletion
- Error handling with detailed error messages
- Results export - creates a CSV report of all operations
- Progress tracking with counters and status updates
- Color-coded console output for easy monitoring
# Install the Exchange Online Management module
Install-Module -Name ExchangeOnlineManagement
# Connect to Exchange Online
Connect-ExchangeOnline -UserPrincipalName admin@yourdomain.com# Add Exchange Management Shell snap-in
Add-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapInThe CSV file should contain at least one of the following columns:
Identity(recommended)EmailAddressEmailPrimarySmtpAddressDisplayNameName
Or with multiple columns:
Identity,DisplayName
[email protected],Sales Team
[email protected],Marketing Distribution List.\Delete-DistributionListsFromCSV.ps1 -CsvFilePath "C:\Lists\DistributionLists.csv".\Delete-DistributionListsFromCSV.ps1 -CsvFilePath "C:\Lists\DistributionLists.csv" -WhatIf.\Delete-DistributionListsFromCSV.ps1 -CsvFilePath "C:\Lists\DistributionLists.csv" -LogPath "C:\Logs\MyLog.log"The script generates two output files:
-
Log File:
DeleteDL_YYYYMMDD_HHMMSS.log- Contains detailed timestamped logs of all operations
- Includes success, failure, and error messages
-
Results CSV:
DistributionLists_Results_YYYYMMDD_HHMMSS.csv- Contains a record of each distribution list processed
- Includes status (Success/Failed/Skipped) and messages
| Parameter | Required | Description |
|---|---|---|
CsvFilePath |
Yes | Path to the CSV file containing distribution lists to delete |
LogPath |
No | Custom path for the log file (default: current directory with timestamp) |
WhatIf |
No | Test mode - shows what would be deleted without making changes |
-
Export existing distribution lists (optional):
Get-DistributionGroup | Select-Object Identity,DisplayName,PrimarySmtpAddress | Export-Csv -Path "AllDLs.csv" -NoTypeInformation
-
Edit CSV to include only the distribution lists you want to delete
-
Test with WhatIf:
.\Delete-DistributionListsFromCSV.ps1 -CsvFilePath "AllDLs.csv" -WhatIf
-
Execute deletion:
.\Delete-DistributionListsFromCSV.ps1 -CsvFilePath "AllDLs.csv"
-
Review results in the generated log and results CSV files
The script handles various scenarios:
- Missing Exchange connection: Prompts to connect
- Empty CSV: Validates file has content
- Invalid identifiers: Logs and continues with next entry
- Non-existent distribution lists: Logs error and continues
- Throttling: Includes small delays between operations
- WhatIf support: Test before executing
- Confirmation bypass: Uses
-Confirm:$falseonly in actual deletion - Detailed logging: Every action is logged
- Results tracking: Creates comprehensive report of all operations
- Ensure you're connected to Exchange Online or on-premises Exchange
- Run
Connect-ExchangeOnlineor add the Exchange snap-in
- Check your CSV has one of the supported column names
- Rename your column to
Identityfor best compatibility
- Ensure your account has permissions to delete distribution lists
- Typically requires "Organization Management" or "Recipient Management" role
- v1.0 - Initial release with CSV-based bulk deletion functionality
MIT License - Free to use and modify