Powershell script to create an shared mailbox in hybid

lalajee 1,821 Reputation points
2023-02-16T16:32:29.0333333+00:00

Hi,

I like to use an script to create an shared mailbox in exchange hybid

We have Exchange 2016 on-prem server and Exchange online

I have following script but it doesnt assign an permission


$DisplayName = Read-Host "Enter Shared Mailbox Name"
$UPN = $DisplayName -replace "#|-|\s+",""
$SamAccount = $UPN.Substring(0, [Math]::Min($UPN.Length, 20))
$Alias = $DisplayName -replace "#|-",""
$Alias = $Alias -replace "\s+","_"
$GroupName = ("SM_" + $Alias)

    
$OUSharedSMG = "Shared Mailbox group"
$OUSharedMail = "Shared Mailbox (Accounts)"

$group = New-DistributionGroup -Type Security -OrganizationalUnit $OUSharedSMG -Name $GroupName
if($group) { Set-DistributionGroup $GroupName -HiddenFromAddressListsEnabled $true }

# Create mailbox
$mailbox = New-RemoteMailbox -Name $DisplayName -Alias $Alias -OnPremisesOrganizationalUnit $OUSharedMail -Shared -UserPrincipalName "$UPN" `
-SamAccountName $SamAccount

Add-MailboxPermission -Identity $Alias `
-User $GroupName -AccessRights FullAccess -InheritanceType All
Exchange Online
Exchange Online
A Microsoft email and calendaring hosted service.
6,178 questions
Exchange | Hybrid management
Windows for business | Windows Server | User experience | PowerShell
{count} votes

2 answers

Sort by: Most helpful
  1. Fabricio Godoy 2,626 Reputation points
    2023-02-16T16:49:05.1166667+00:00

    Hello @lalajee

    i believe u are looking for this.

    # Connect to Exchange on-premises
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://<your on-premises Exchange server>/PowerShell/ -Authentication Kerberos
    Import-PSSession $Session
    
    # Prompt for shared mailbox name and member email addresses
    $SharedMailboxName = Read-Host "Enter the name of the shared mailbox"
    $MemberEmails = Read-Host "Enter the email addresses of the members separated by a semicolon (;)"
    
    # Create shared mailbox on-premises
    New-Mailbox -Name $SharedMailboxName -Alias $SharedMailboxName -Shared
    
    # Add members to shared mailbox on-premises
    $MemberEmailsArray = $MemberEmails.Split(';')
    ForEach ($MemberEmail in $MemberEmailsArray) {
        Add-MailboxPermission -Identity $SharedMailboxName -User $MemberEmail -AccessRights FullAccess -InheritanceType All
    }
    
    # Synchronize shared mailbox to Exchange Online
    New-MoveRequest -Identity $SharedMailboxName -RemoteLegacy -RemoteGlobalCatalog "<your on-premises domain controller>" -TargetDeliveryDomain "<your domain>.onmicrosoft.com" -TargetDatabase "<your on-premises mailbox database>"
    
    # Disconnect from Exchange on-premises
    Remove-PSSession $Session
    
    

    I hope this is work for u.

    and please, don`t forget to upvote if this help u.

    Regards


  2. Andy David - MVP 157.8K Reputation points MVP Volunteer Moderator
    2023-02-16T18:20:13.56+00:00

    If you are creating a remote shared mailbox, then you will need to connect to Exchange Online to set the perms:

    so for your menu, you will want to install the Exchange Online Module, then run

    Add-MailboxPermission -Identity $Alias ` -User $GroupName -AccessRights FullAccess -InheritanceType All
    

    https://learn.microsoft.com/en-us/powershell/exchange/connect-to-exchange-online-powershell?view=exchange-ps#step-1-load-the-exchange-online-powershell-module

    You can use a prefix to ensure it doesnt conflict with on-prem Exchange powershell

    Example: Connect-ExchangeOnline -prefix "365"

    Then the command in your menu is:

    Add-365MailboxPermission -Identity $Alias ` -User $GroupName -AccessRights FullAccess -InheritanceType All
    

    https://learn.microsoft.com/en-us/powershell/module/exchange/connect-exchangeonline?view=exchange-ps

    User's image


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.