public/Start-O365ConvertToShared.ps1

<#
.Synopsis
This powershell cmdlet will aid you in the process of converting a 'DirSyncd' users mailbox into a shared mailbox on Office 365. The process carries out the following steps:
1) Moves user to unsynced OU (you must provide an unsynced OU)
2) Forces an Azure AD Full Sync for Office 365 to mark the mailbox as deleted
3) Restores the Synced users mailbox in the cloud
4) Sets the mailbox type to Shared
5) Clears the ImmutableID associated with the newly coverted mailbox to avoid Sync Tasks from re-deleting the mailbox
  
.Description
This function helps automate the process of converting a synced users mailbox to a shared mailbox in Office 365
  
.Example
Convert-O365UsertoShared -Username "jbond"
#>

Function Start-O365ConvertToShared{
    [cmdletbinding()]
    Param (
    [Parameter(Position=0,mandatory=$true)] [string]$Username 
    )
    Process {
            Test-O365Connection
            Test-O365DirSync
            
            #Setting Variables based on the Username input for the required parameters
            $Email = Get-ADUser -Identity $Username -Properties ProxyAddresses | select -ExpandProperty ProxyAddresses | ? {$_ -clike "SMTP:*"}
            $Email = $Email -replace "SMTP:"
            $OU = Get-ADOrganizationalUnit -Filter * | Select -ExpandProperty DistinguishedName |Out-GridView -Title "Select Organizational Unit to move user to:" -PassThru
            
            #Actions taken on your local domain to move the user to an unsynced OU
            Write-Host "Moving user to specified OU: $OU" -ForegroundColor Green
            Get-ADUser $Username| Move-ADObject -TargetPath "$OU"
            Start-O365DirSync -SyncType Full
            
            #Checking after Sync to confirm the mailbox is 'Soft Deleted' in Office 365
            Write-Host "Waiting for Office 365 to register the mailbox cahnges ..." -NoNewline -ForegroundColor Green
            $x = 1
            While($x -ne 12){
                Start-Sleep -Seconds 5
                Write-Host "." -NoNewline -ForegroundColor Green
                $x++ 
                }
            If(Get-Mailbox -SoftDeletedMailbox $email){ Write-Host "User mailbox has been deleted successfully" -ForegroundColor Green}
            Else{
            Write-Host "ERROR - Mailbox not found in query of Office 365 'Soft Deleted' mailboxes. Please check mailbox and try again." -ForegroundColor RED
            return
            } 
            
            #Restoring mailbox as a Cloud-based mailbox
            Restore-MsolUser -UserPrincipalName $Email
            Write-Host "Waiting for Office 365 to register the mailbox changes ..." -NoNewline -ForegroundColor Green
            $x = 1
            While($x -ne 12){
                Start-Sleep -Seconds 5
                Write-Host "." -NoNewline -ForegroundColor Green
                $x++ 
                }

            #Applying settings to convert to a Shared Mailbox
            If(Get-Mailbox $email){
                Set-Mailbox $Email -Type shared
                Set-msoluser -userprincipalname $Email -ImmutableID ""
                }
            Else{
            Write-Host "Mailbox not restored properly - please check logs and try again." -ForegroundColor Red
            return
            }
       }
       #End Process
}