Automatic license assignment

To be more efficient, cost-effective and adapted to their business needs, a client asked me to create a script to automate the assignment of user licenses on Office 365. In a collegial environment, where all users have the same licenses, it is quite simple to create a PowerShell script (see below) and to program it with task scheduler on a company server.

Here the script will automatically associate a first Plan E1 license that includes Exchange, Sharepoint and Lync options. Next, the script will add the Office365Pro Plus license which allows students to install the Office suite on their personal computer.

Before you can use the script below some prerequisites:

  • Microsoft Online Service Sign-in Assistant must be installed on your server
  • The MsOnline PowerShell Module(32-bit or 64-bit) must also be installed on the server
  • You must use a Global Administrator account for your Office 365 tenant
  • Finally, don’t forget to encrypt your password
Script.ps1

#Variables
$StudentE1Plan = “CONTOSO:STANDARDWOFFPACK_STUDENT”
$StudentProPlusPlan = “CONTOSO:OFFICESUBSCRIPTION_STUDENT”
$UsageLocation = “CA”
$AdminUsername = “Admin.Online@contoso.onmicrosoft.com”
$AdminPassword = “01234560d08c9ddf0115d1118c7a00c04fc297eb01000000624cd458111cce43914700864c926d6c0000000002000000000003660000c000000010000000d1978bc3e6f882a365ee88cf99d218a90000000004800000a000000010000000bf53352b415bdad87713c3024126198a18000000b50d34af8581989a86e484b7ef743fd0a7b7dcc6dbbe2b2e1400000065d00a033b86dcb5d44e739882db27318e17c546”

#Conversions
$SecurePassword = ConvertTo-SecureString $AdminPassword – AsPlainText – Force
$cred = New-Object – TypeName System . Management . Automation . PSCredential – argumentlist $AdminUsername,$SecurePassword

#Import MsOnline Module
Import-Module MSOnline

#Connect to Office365
Connect-MSOLService – Credential $cred

#Get Unlicenced Users on Office365
$SudentE1Options = New-MsolLicenseOptions – AccountSkuId $StudentE1Plan
$SudentProPlusOptions = New-MsolLicenseOptions – AccountSkuId $StudentProPlusPlan
$UnlicencedUsers = Get-MSOLUser – UnlicensedUsersOnly – All

#Set Location CA for CANADA and Student A2 licence for all unlicenced users
$UnlicencedUsers | ForEach-Object {
Set-MsolUser – UserPrincipalName $_.UserPrincipalName – UsageLocation $UsageLocation
Set-MsolUserLicense – UserPrincipalName $_.UserPrincipalName – AddLicenses $StudentE1Plan – LicenseOptions $SudentE1Options
Set-MsolUserLicense – UserPrincipalName $_.UserPrincipalName – AddLicenses $StudentProPlusPlan – LicenseOptions $StudentProPlusPlan
}

Leave a Reply

Your email address will not be published. Required fields are marked *

Share this article

Want to migrate or connect your phone system to Teams?

Write to me and let's keep in touch!

small_c_popup.png

Contact form

Let's discuss your projects together

small_c_popup.png

Contact form

Let's discuss your projects together