Skip to content

Instantly share code, notes, and snippets.

Created December 12, 2017 11:21
What would you like to do?
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 = ''
$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 = 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}' -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
Set-MsolUser -UserPrincipalName $paramEXOMailBox.MicrosoftOnlineServicesID -UsageLocation "NL"
Set-MsolUserLicense -UserPrincipalName $paramEXOMailBox.MicrosoftOnlineServicesID -AddLicenses $SkuId
#region Disconnect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment