RedditPP Mascot

RedditPP

r/PowerShell • … • u/pjkm123987

Script sharing: Adding multiple email groups to a user and adding multiple users to a email group/shared mailbox in Office365

Sharing a script if you ever needed to add lots of email groups/security groups to a user, just need to replace the credential email with your admin email. Run the script, put in the group names seperated with a "," for multiple groups, then enter the user name/email address to add the entered groups to.

# Connect to Exchange Online Connect-ExchangeOnline -UserPrincipalName [email protected] $credentials = Get-Credential [email protected] Connect-AzureAD -Credential $credentials

# Set the group email addresses that the user needs to be added to $groupEmailAddresseslist = Read-Host "Enter the group to add to add to the user (comma-separated no spaces for multiple)" $groupEmailAddresses = $groupEmailAddresseslist -split ","

$groupList = $groupEmailAddresses -join "`n`r" Write-Host "Adding groups:`n`r$groupList" -ForegroundColor blue

# Define the email address you want to check $userEmail = Read-Host "Enter user email to add membership"

# Iterate over each group email address foreach ($groupEmail in $groupEmailAddresses) { # Check if the email address is a mail enabled security group or distribution group $group = Get-DistributionGroup -Identity $groupEmail -ErrorAction SilentlyContinue if ($group -ne $null) { if ($group.RecipientTypeDetails -eq "MailUniversalDistributionGroup" -or $group.RecipientTypeDetails -eq "MailUniversalSecurityGroup") { try { Add-DistributionGroupMember -Identity $group.Name -Member $userEmail -BypassSecurityGroupManagerCheck -ErrorAction Stop Write-Host "Added user to distribution group: $groupEmail" -ForegroundColor Green } catch { # Handle any exceptions here if needed Write-Host "Error occurred while adding user to " -ForegroundColor Red -nonewline; Write-Host "$groupEmail" -ForegroundColor Cyan -nonewline; Write-Host " group: $_" -ForegroundColor Red } } } else { # Check if the email address is a Microsoft 365 group $group = Get-UnifiedGroup -Identity $groupEmail -ErrorAction SilentlyContinue if ($group -ne $null) { if ($group.RecipientTypeDetails -eq "GroupMailbox") { try { Add-UnifiedGroupLinks -Identity $group.Id -LinkType Members -Links $userEmail -ErrorAction Stop Write-Host "Added user to Microsoft 365 group: $groupEmail" -ForegroundColor Cyan } catch { # Handle any exceptions here if needed Write-Host "Error occurred while adding user to " -ForegroundColor Red -nonewline; Write-Host "$groupEmail" -ForegroundColor Cyan -nonewline; Write-Host " group: $_" -ForegroundColor Red } } } else { $Group = Get-AzureADGroup -SearchString $groupEmail -ErrorAction SilentlyContinue $user = Get-AzureADUser -Searchstring $userEmail if ($group -ne $null) { try { Add-AzureADGroupMember -ObjectId $group.ObjectId -RefObjectId $user.ObjectId Write-Host "Added user to security group: $groupEmail" -ForegroundColor Magenta } catch { # Handle any exceptions here if needed Write-Host "Error occurred while adding user to " -ForegroundColor Red -nonewline; Write-Host "$groupEmail" -ForegroundColor Cyan -nonewline; Write-Host " group: $_" -ForegroundColor Red Write-Host $Group } } } } } Read-Host -Prompt "Press Enter to exit"

Another script to add multiple users to a single email group or shared mailbox, works similar to the first powershell script.

# Connect to Exchange Online Connect-ExchangeOnline -UserPrincipalName [email protected] $credentials = Get-Credential [email protected] Connect-AzureAD -Credential $credentials

# Set the group name to add users to $GroupName = Read-Host "Enter the group email or shared mailbox to add users" Write-Host "Target group - $GroupName" -ForegroundColor cyan

# Set display name or primary email to add to the group $UserListNames = Read-Host "Enter the user's to add to group/mailbox (comma-separated no spaces for multiple)" $UserList = $UserListNames -split ','

$Userlisted = $UserList -join "`n`r" Write-Host "Listed users to add: `n`r$Userlisted" -ForegroundColor cyan

# Add each user to the distribution group $group = Get-DistributionGroup -Identity $GroupName -ErrorAction SilentlyContinue if ($group -ne $null) { foreach ($User in $UserList) { try { Add-DistributionGroupMember -Identity $GroupName -Member $User -BypassSecurityGroupManagerCheck -Confirm:$false Write-Host "Added $User to distribution group: $GroupName" -ForegroundColor Green } catch { Write-Host "Error occurred while adding " -ForegroundColor Red -nonewline; Write-Host "$User" -ForegroundColor Cyan -nonewline; Write-Host " to the group: $_" -ForegroundColor Red } } } else { $group = Get-UnifiedGroup -Identity $GroupName -ErrorAction SilentlyContinue if ($group -ne $null) { foreach ($User in $UserList) { try { Add-UnifiedGroupLinks -Identity $GroupName -LinkType Members -Links $User -ErrorAction Stop Write-Host "Added $User to Microsoft 365 group: $GroupName" -ForegroundColor Cyan } catch { Write-Host "Error occurred while adding " -ForegroundColor Red -nonewline; Write-Host "$User" -ForegroundColor Cyan -nonewline; Write-Host " to the group: $_" -ForegroundColor Red } } } else { $Group = Get-AzureADGroup -SearchString $GroupName -ErrorAction SilentlyContinue if ($group -ne $null) { foreach ($User in $UserList) { $AzureADUser = Get-AzureADUser -Filter "displayName eq '$User'" -ErrorAction SilentlyContinue if ($AzureADUser -ne $null) { try { Add-AzureADGroupMember -ObjectId $Group.ObjectId -RefObjectId $AzureADUser.ObjectId Write-Host "Added $User to security group: $GroupName" -ForegroundColor Magenta } catch { Write-Host "Error occurred while adding " -ForegroundColor Red -nonewline; Write-Host "$User" -ForegroundColor Cyan -nonewline; Write-Host " to $GroupName security group: $_" -ForegroundColor Red } } else { Write-Host "User '$User' not found in Azure AD." -ForegroundColor Red } } } else { foreach ($User in $UserList) { try { Add-MailboxPermission -Identity $GroupName -User $User -AccessRights FullAccess -InheritanceType All Add-RecipientPermission -Identity $GroupName -Trustee $User -AccessRights SendAs -Confirm:$false Write-Host "Adding $User access permissions to $GroupName" -ForegroundColor Green } catch { Write-Host "Error occurred while adding " -ForegroundColor Red -nonewline; Write-Host "$User" -ForegroundColor Cyan -nonewline; Write-Host " to $GroupName shared mailbox: $_" -ForegroundColor Red } } } } }

Read-Host -Prompt "Press Enter to exit"

​

— 0