Powershell add mailboxes to group automatically

Created by Paul Mendoza, Modified on Thu, 10 Nov 2022 at 12:18 PM by Paul Mendoza

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 Distirbution 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.
    
}


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article