Back to blog

Automatically Assign Licences to New Office 365 Users created with DirSync

Sep 3, 2013 by Emma Robinson

Office 365 users that are newly created during synchronization from an On-Premise Active Directory are not assigned an Office 365 licence by default. We recently came across a customer who wanted all newly created Office 365 to automatically have a licence assigned to them.

We came up with a script that they could schedule on a local server in their Active Directory that automatically assigned a default licence to these newly created users. The script can be run as a scheduled task from a server in your on-premise Active Directory and it will automatically connect to Office 365 and assign a licence to all unlicensed users.

Before you can use the script you will need to know what type of licence to apply. To get this information, connect to Office 365 using PowerShell and run the Get-MSOLAccountSku cmdlet.

PS C:\Users\burns_000\desktop> Get-MsolAccountSku
AccountSkuId ActiveUnits WarningUnits ConsumedUnits
------------ ----------- ------------ -------------
alantest8:ENTERPRISEPACK 25 0 25
PS C:\Users\burns_000\desktop>

Copy the text under AccountSkuId for the licence type that you want to apply and paste it into the script below for the $AccountSku variable

$AccountSkuId = "alantest8:ENTERPRISEPACK"

You also need to set your users default Usage Location, which is the two letter code associated with your country.

$UsageLocation = "GB"

Then it’s simply a matter of filling in your administrative username and password for Office 365

$AdminUsername = “”
$AdminPassword = “Password”

So go ahead and copy the text below into a file and save it as AssignLicences.ps1 in a folder called c:\scripts.

Then create a new scheduled task and have it run powershell.exe -noexit c:\scripts\AssignLicences.ps1 every 15 minutes.

#Change these variables to match your environment
$AccountSkuId = "alantest8:ENTERPRISEPACK"
$UsageLocation = "GB"
$AdminUsername = ""
$AdminPassword = "Password"
$SecurePassword = ConvertTo-SecureString $AdminPassword -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $AdminUsername,$SecurePassword
Connect-MSOLService -Credential $cred
$LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId
$UnlicencedUsers = Get-MSOLUser -UnlicensedUsersOnly -All
$UnlicencedUsers | ForEach-Object {
Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation $UsageLocation
Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses $AccountSkuId -LicenseOptions $LicenseOptions

Obviously this is only suitable for small environments with one licence type.

Instead of running all this PowerShell, why not sign up to a free trial of our Office 365 reporting software and we can do the hard work for you!