YaExOs (Yet another Exchange Online script)

Sup’ PSHomies,

Exchange online… I’ll be honest… In my many years in IT, I avoided Exchange like the plague! I’ve seen many colleagues crumble under it’s weight! (Sounds more dramatic don’t you think?) But with Office 365 being all the rage now, I guess I better get with the program, so here goes…

So there was recently a bit of a commotion on twitter about sharing level 200 stuff. We all were beginners at some point, so if you already know this then I guess this blog wasn’t meant for you… πŸ˜‰

So how do you go about creating a mailbox for a user in Office 365 Exchange? Using PowerShell of course… Hehe…

Ok there are modules for AzureAD and MSOnline and stuff but where are the Exchange Online cmdlets? That was my first step. Luckily I found an excellent blog on how to go about doing this. The rest after that is pretty straightforward…

Looking at the cmdlet I wanted to figure out if there were any mandatory attributes.

(get-command New-OnlineMailbox).ParameterSets.Parameters | Out-GridView


No mandatory parameter(s)… I’ll just stick with the basics on this one:

  • Alias
  • Name
  • FirstName
  • LastName
  • DisplayName

Feel free to add any other parameters you need… πŸ˜‰

You’ll need Global Admin credentials to create your Mailbox Online so have that at hand…

Once the Mailbox is created you’ll need to assign a license to the user. I’m using the MSOL cmdlet for this just because it’s still around (and way easier to implement). Having said that here’s a link to how it’s done using AzureAD cmdlets. That practical365 site is definitely a good source for anything Office365 related! πŸ˜‰ Oh and the book is definitely worth having as a reference too!

Ok here’s the script:

Author: I. Strachan
Version: 1.0
Version History:
Purpose: Import Exchange Online cmdlets and create a New Mailbox
$Alias = 'irwins',
$Name = 'irwins',
$FirstName = 'Irwin',
$LastName = 'Strachan',
$DisplayName = 'Irwin Strachan'
#region Exchange Online Functions
function Connect-EXOnline {
$Url = 'https://ps.outlook.com/powershell'
$paramEXOSession = @{
ConfigurationName = 'Microsoft.Exchange'
ConnectionUri = $Url
Credential = $EXOnlineCred
Authentication = 'Basic'
AllowRedirection = $true
Name = 'Exchange Online'
$EXOSession = New-PSSession @paramEXOSession
Import-PSSession $EXOSession -Prefix 'Online'
function Disconnect-EXOnline {
Remove-PSSession -Name 'Exchange Online'
#region Get Credentials and Connect To Exchange Online
$paramCredential = @{
Message = 'Enter Global O653 Admin account'
UserName = 'o365admin@yourtenant.onmicrosoft.com'
$o365Admin = Get-Credential @paramCredential
Connect-EXOnline -EXOnlineCred $o365Admin
Connect-MsolService -Credential $o365Admin
#region Create a new Mailbox
$paramEXOMailBox = @{
Alias = $Alias
Name = $Name
FirstName = $FirstName
LastName = $LastName
DisplayName = $DisplayName
MicrosoftOnlineServicesID = '{0}@yourtenant.onmicrosoft.com' -f $Alias
Password = $(ConvertTo-SecureString -String 'P@ssw0rd' -AsPlainText -Force)
ResetPasswordOnNextLogon = $true
New-OnlineMailbox @paramEXOMailBox
#region Assign new Mailbox a license
#Get AccountSku. In my case I only have one
$SkuId = (Get-MsolAccountSku).AccountSkuId
#Get All unlicensed users
Get-MsolUser -All -UnlicensedUsersOnly
#Set MSOL User License
#Alternatively have a look at https://practical365.com/blog/managing-office-365-licenses-with-azure-ad-v2-powershell-module/
Set-MsolUser -UserPrincipalName $paramEXOMailBox.MicrosoftOnlineServicesID -UsageLocation "NL"
Set-MsolUserLicense -UserPrincipalName $paramEXOMailBox.MicrosoftOnlineServicesID -AddLicenses $SkuId
#region Disconnect

So that’s how to get started with Exchange Online.

I’ll leave you with a lil’ thought from the Austrian Oak himself…


Hope it’s worth something to you,






Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s