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

[cc lang="powershell"]Start-Transcript -Path $env:LOCALAPPDATA"CloudwyseLogs"$(get-date -Format ddMMyy_HHmmss)".log" -NoClobber[/cc]

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

[cc lang="powershell"]Clear-Host[/cc]

Prompt user to enter the details of the new account

[cc lang="powershell"]$FirstName = Read-Host -Prompt "Carefully type the user's First name"
$SecondName = Read-Host -Prompt "Carefully type the user's Second name"
[/cc]

Format the AD fields in the right way

[cc lang="powershell"]$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)"
[/cc]

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

[cc lang="powershell"]$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)"
[/cc]

Connect to on-prem exchange and create mailbox

[cc lang="powershell"]$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"[/cc]

Disconnect from exchange and stop logging

[cc lang="powershell"]Remove-PSSession $Session
write-host -ForegroundColor Green "The script completed, any errors will appear in the log file"
Stop-Transcript[/cc]