Skip to content

Instantly share code, notes, and snippets.

@irwins
Created December 12, 2017 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save irwins/2ecacd8b77498d4d1c0c12b4ca92f704 to your computer and use it in GitHub Desktop.
Save irwins/2ecacd8b77498d4d1c0c12b4ca92f704 to your computer and use it in GitHub Desktop.
<#
Author: I. Strachan
Version: 1.0
Version History:
Purpose: Import Exchange Online cmdlets and create a New Mailbox
#>
param(
[String]
$Alias = 'irwins',
[String]
$Name = 'irwins',
[String]
$FirstName = 'Irwin',
[String]
$LastName = 'Strachan',
[String]
$DisplayName = 'Irwin Strachan'
)
#region Exchange Online Functions
#https://practical365.com/exchange-server/powershell-function-connect-office-365/
function Connect-EXOnline {
Param(
[PSCredential]$EXOnlineCred
)
$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'
}
#endregion
#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
#endregion
#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
#endregion
#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
#endregion
#region Disconnect
Disconnect-EXOnline
#endregion
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment