ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    So you need to bulk assign Office 365 licenses to specific set of users using PowerShell

    IT Discussion
    1
    1
    952
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • GregoryHallG
      GregoryHall
      last edited by GregoryHall

      Seems like all I do lately is migrate users to Office 365. In that I have started getting very large numbers of users and it quickly became impractical for me to manually assign Office 365 licenses. In that regard I started looking around for some PowerShell scripts that would allow me to do this. After searching and searching it appears that most, if not all, of the examples on the web were for creating new users then assigning the licenses to them. I had already setup DirSync so the users were already populated on Office 365, so that method was not going to work.

      So after much trial and error I have come up with a Script you can run against a email address list CSV dump that will assign the license for you in bulk. Some of the steps are well known, like connecting PowerShell remotely to Office 365, but the bit that had me stumped was now to set the license without actually creating the user first. Once I got around that I was home free.

      Steps

      Dump your user email list to a CSV text file and set the header for the one column to UserPrincipalName and put the users email address under it one line per user like the example below.

      UserPrincipalName
      [email protected]
      [email protected]
      etc…

      • Install Microsoft Online Services Sign-In Assistant
      • http://www.microsoft.com/en-us/download/details.aspx?id=41950
      • Install Azure AD Module
      • http://go.microsoft.com/fwlink/p/?linkid=236297
      • Find the Azure AD PowerShell Icon and right click then Run As Administrator
      • Go to your C:\ and create a folder called Scripts

      Create a new text file and copy the following code into it, then save the file as licenses.ps1 and save it to the scripts folder.

      Connect-MsolService
      #CSV file picker module start
      Function Get-FileName($initialDirectory)
      { 
       [System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) |
       Out-Null
        
       $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
       $OpenFileDialog.initialDirectory = $initialDirectory
       $OpenFileDialog.filter = “All files (*.*)| *.*”
       $OpenFileDialog.ShowDialog() | Out-Null
       $OpenFileDialog.filename
      }
      
      #CSV file picker module end
      
      #Variable that holds CSV file location from file picker
      $path = Get-FileName -initialDirectory “c:\”
      
      #Window with list of available 365 licenses and their names
      Get-MsolAccountSku | out-gridview
      
      #Input window where you provide the license package’s name
      $server = read-host ‘Provide licensename (AccountSkuId)’
      
      #CSV import command and mailbox creation loop
      import-csv $path | foreach {
      Set-MsolUser -UserPrincipalName $_.UserPrincipalName -usagelocation “US”
      Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses “$server”
      }
      
      #Result report on licenses assigned to imported users
      import-csv $path | Get-MSOLUser | out-gridview
      

      Jump back to the Azure PowerShell window and set the execution policy to unrestricted by running the following command in the PowerShell window

      • Set-ExecutionPolicy Unrestricted
      • Hit “Y” when prompted
      • CD C:\scripts
      • Run the script
      • .\licenses.ps1

      First prompt is for your Office 365 admin credentials. Use the [email protected] account that was created the first time you setup Office 365.

      Second Prompt is for the location of the UserPrincipalName CSV file, just browse to where you saved it (c:\scripts) and select it then hit ok.

      Third window will look up the sku of the licenses you purchased.
      Type the SKU name EXACTLY as you see it with your account name :then the SKU

      Then the script will run and assign the licenses to the users.

      In conclusion I hope this helps you with your Office 365 migrations.

      References:
      http://www.codetwo.com/admins-blog/how-to-add-and-license-users-in-bulk-on-office-365/

      1 Reply Last reply Reply Quote 5
      • 1 / 1
      • First post
        Last post