I recently wrote this script that creates an O365 mailbox within a hybrid exchange environment, creating all the necessary attributes etc in exchange and AD

Log all output to a local logfile, that is unique each time and won’t get overwritten


Start-Transcript -Path $env:LOCALAPPDATA"CloudwyseLogs"$(get-date -Format ddMMyy_HHmmss)".log" -NoClobber

Clear output from the screen to prevent previous command being sent as first name


Clear-Host

Prompt user to enter the details of the new account


$FirstName = Read-Host -Prompt "Carefully type the user`'s First name"
$SecondName = Read-Host -Prompt "Carefully type the user`'s Second name"

Format the AD fields in the right way


$UserName = $FirstName.ToLower() + "." + $SecondName.ToLower()
$DisplayName = (Get-Culture).TextInfo.ToTitleCase($FirstName.ToLower() + " " + $SecondName.ToLower())
$ProperFName = (Get-Culture).TextInfo.ToTitleCase($FirstName.ToLower())
$ProperSName = (Get-Culture).TextInfo.ToTitleCase($SecondName.ToLower())
$UPN = $UserName + "@contoso.com"
write-host -ForegroundColor Green "Your User`'s DisplayName is $($DisplayName)"
write-host -ForegroundColor Green "Your User`'s Username is $($UserName)"

Get credentials for the new user account and the connection to the on-prem exchange server


$UserCreds = Get-Credential -UserName "DO NOT EDIT THIS FIELD" -Message "Please set the password for the new user"
$ExchangeCreds = Get-Credential -UserName "CONTOSO`" -Message "Please enter your credentials which are required to connect to CONEXCH01"
write-host -ForegroundColor Green "
Your User`'s UPN is $($UPN)"

Connect to on-prem exchange and create mailbox


$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://CONEXCH01.contoso.com/PowerShell/ -Authentication Kerberos -Credential $ExchangeCreds
Import-PSSession $Session
New-RemoteMailbox -Name $DisplayName -FirstName $ProperFName -LastName $ProperSName -Password $UserCreds.Password -UserPrincipalName $UPN -OnPremisesOrganizationalUnit "contoso.com/CONTOSO Users/Swap"

Disconnect from exchange and stop logging


Remove-PSSession $Session
write-host -ForegroundColor Green "The script completed, any errors will appear in the log file"
Stop-Transcript