Untitled
unknown
plain_text
10 months ago
1.8 kB
8
Indexable
# Install the required module (if not already installed)
Install-Module -Name ExchangeOnlineManagement -Force -AllowClobber
# Import the required module
Import-Module ExchangeOnlineManagement
# Connect to Exchange Online
$UserCredential = Get-Credential
Connect-ExchangeOnline -Credential $UserCredential
# Specify the path to the text file containing UPNs
$TextFilePath = "C:\Path\To\Your\Text\File.txt"
# Specify the name of the distribution list
$DistributionListName = "YourDistributionList"
# Specify the path to the output text file
$OutputFilePath = "C:\Path\To\Output\File.txt"
# Read UPNs from the text file
$UserUPNs = Get-Content -Path $TextFilePath
# Initialize a list to collect UPNs of users who were added to the distribution list
$AddedUsers = @()
# Add users to the distribution list
foreach ($UserUPN in $UserUPNs) {
# Check if the user is already a member of the distribution list
$IsMember = Get-DistributionGroupMember -Identity $DistributionListName | Where-Object { $_.PrimarySmtpAddress -eq $UserUPN }
if (-not $IsMember) {
# Add the user to the distribution list
Add-DistributionGroupMember -Identity $DistributionListName -Member $UserUPN
Write-Host "User $($UserUPN) added to $($DistributionListName) successfully."
# Add the UPN to the list of added users
$AddedUsers += $UserUPN
} else {
Write-Host "User $($UserUPN) is already a member of $($DistributionListName)."
}
}
# Write the list of added users to the output text file
$AddedUsers | Out-File -FilePath $OutputFilePath
Write-Host "The UPNs of the added users have been exported to $OutputFilePath."
# Disconnect from Exchange Online when done
Disconnect-ExchangeOnline -Confirm:$falseEditor is loading...
Leave a Comment