Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PowerShell script to copy O365 distribution lists from a mailbox to another
# Connect to O365 via PowerShell
$Cred = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell/ -Credential $Cred -Authentication Basic –AllowRedirection
Import-PSSession $Session
# Copy the distribution lists
# replace developer-role@example.com with the email from which you want to copy the DL's
# replace new-user@example.com with the email to which you want to copy the DL's
$dists= Get-DistributionGroup
$sourceMailbox = Get-Mailbox 'developer-role@example.com'
foreach($dist in $dists){
$gms = Get-DistributionGroupMember -identity $dist.Identity
foreach ($gm in $gms){
if ($gm.name -eq $sourceMailbox.name){
write-host 'User Found In Group' $dist.Alias
Add-DistributionGroupMember -Identity $dist.Alias -Member "new-user@example.com"
}
}
}
# Disconnect from O365
Remove-PSSession $Session
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment