Skip to main content

Microsoft 365 Admin - Use PowerShell to add Mailboxes to a Group Automatically

Updated over 10 months ago

If you want to have Powershell automatically manage lists of mailboxes these scripts can help with that process. You'll still need to tweak them a bit to get them right.


Office 365 - Add to Distribution Group

Create a text file with one email address per line. Then run this. Modify the file path and the group name

#Need to use module ExchangePowerShell
Install-Module -Name ExchangePowerShell -Scope CurrentUser -Repository PSGallery -Force
Write-Host "Login to the Exchange account where your Groups are set. We'll use this to add groups."
Connect-ExchangeOnline

# You'll need to change the filename
# The file should have one email address per line


foreach($line in Get-Content C:\Users\pmend\Desktop\users.txt) {
# https://docs.microsoft.com/en-us/powershell/module/exchange/add-distributiongroupmember?view=exchange-ps
# -Identidy = Group we want to add the user to. Can be Name, Alias, Distinguished Name, Canonical DN, Email address or GUID
Add-DistributionGroupMember -Identity "My Group" -Member $line
Write-Output -InputObject $line
}

Exchange - Find contacts in Distribution Group and add to mailboxes.txt file

For Exchange this will loop thru the members of a distribution group and add them to the mailboxes.txt file.

#Need to use module ExchangePowerShell
Install-Module -Name ExchangePowerShell -Scope CurrentUser -Repository PSGallery -Force
Write-Host "Login to the Exchange account where your Groups are set. We'll use this to add groups."
Connect-ExchangeOnline


#remove the mailboxes.txt file
Remove-Item -Path C:\sigparser\mailboxes.txt

foreach($member in Get-DistributionGroupMember -Identity "SigParser Mailboxes") {
Write-Output -InputObject $member
$name = $member.Name
Add-Content C:\sigparser\mailboxes.txt "$name"
#TODO: Grant delegated read access to mailbox.

}

If you have any questions, send us an email at support@sigparser.com. You can also book a demo session HERE.


​


​

Did this answer your question?