Can I assign a license to a group of users by PowerShell?

  • 4 Followers
  • 10 Replies |
  • This post has 2 verified answers |
Answered (Verified) This question is answered

Our Company has many O365 users say more than 5000 users. Can I write a PowerShell script to check a attribute in user account, that if the attribute equal to O365 than the user account will be auto assigned an O365 license.

 

Is this possible?  Or how can I assign a license to a group of users in a same time?

  • Post Points: 20
Verified Answer
  • hi Poly, you can do this using a CSV with a single column that contains the users UPN. If you want to bulk activate you can run the same script but change the variable input. First you have to get the license name, to do this:

    Connect-MsolService

    Get-MsolAccountSku

    That will output your sku's. Once you have that you would run a script like this:

    Connect-MSOLService -Credential $adminCredential

    $AccountSkuId = "sku:ENTERPRISEPACK"

    $UsageLocation = "US"

    $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId

    $Users = Import-Csv c:\Users.csv

    $Users | ForEach-Object {

    Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation $UsageLocation

    Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses $AccountSkuId -LicenseOptions $LicenseOptions

    }

    if you wanted to do this for everyone you would change the line:

    $users | Import-Csv c:\Users.csv

    to:

    $users | get-msoluser -resultsize unlimited

    Jorge R. Diaz

    Microsoft MVP | Office 365

    MCITP: Office 365 Administrator

    Check out my Blog

    Follow Me

    • Top 100 Contributor
    • Post Points: 0
  • Yes you can add that part to your script a couple of different ways, the way I do it is by putting this at the top of the script:

    $powerUser = "administrator@domain.com"

    $powerPass = "XXXX"

    $password = ConvertTo-SecureString $powerPass -AsPlainText -Force

    $adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $powerUser,$password

    What this does is allows you to store the creds in the script, then it converts the password so when it passes it along in the process it is encrypted. There are other ways to do it where you can store the username and password in a seperate TXT file but I figure if you are going to save that information why does it matter if it is in the root script or a seperate text file.

    Jorge R. Diaz

    Microsoft MVP | Office 365

    MCITP: Office 365 Administrator

    Check out my Blog

    Follow Me

    • Top 100 Contributor
    • Post Points: 0
All Replies
  • Hello,

    Have you synced all users in your on-premise domain to Office 365? 

    Yes, if users have been synced/created in Office 365, you can connect Windows Powershell to Office 365 and use Set-MsolUserLicense to assign licenses in bulk. For detailed information about how to assign license for users with powershell, please refer to following article.

    Granular license assignment from PowerShell
    http://community.office365.com/en-us/w/officeapps/granular-license-assignment-from-powershell.aspx

    Thank you.

    Jack Sun

    • Top 50 Contributor
    • Post Points: 0
  • Hi Jack,

      Thanks for your answer. If I have many new users (e.g. 1000) then can I use PowerShell to assign licenses for this 1000 users on one time? I don't want to repeat the script for 1000 times.

    Thank You.

    • Not Ranked
    • Post Points: 0
  • hi Poly, you can do this using a CSV with a single column that contains the users UPN. If you want to bulk activate you can run the same script but change the variable input. First you have to get the license name, to do this:

    Connect-MsolService

    Get-MsolAccountSku

    That will output your sku's. Once you have that you would run a script like this:

    Connect-MSOLService -Credential $adminCredential

    $AccountSkuId = "sku:ENTERPRISEPACK"

    $UsageLocation = "US"

    $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId

    $Users = Import-Csv c:\Users.csv

    $Users | ForEach-Object {

    Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation $UsageLocation

    Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses $AccountSkuId -LicenseOptions $LicenseOptions

    }

    if you wanted to do this for everyone you would change the line:

    $users | Import-Csv c:\Users.csv

    to:

    $users | get-msoluser -resultsize unlimited

    Jorge R. Diaz

    Microsoft MVP | Office 365

    MCITP: Office 365 Administrator

    Check out my Blog

    Follow Me

    • Top 100 Contributor
    • Post Points: 0
  • Hi Jorge, Thanks for your reply. I have a question about the script. When I execute the script, user name and password is required to input manually. If I want schedule to execute the script,can I make the script run atomically without user name or password input?

    • Not Ranked
    • Post Points: 0
  • Yes you can add that part to your script a couple of different ways, the way I do it is by putting this at the top of the script:

    $powerUser = "administrator@domain.com"

    $powerPass = "XXXX"

    $password = ConvertTo-SecureString $powerPass -AsPlainText -Force

    $adminCredential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $powerUser,$password

    What this does is allows you to store the creds in the script, then it converts the password so when it passes it along in the process it is encrypted. There are other ways to do it where you can store the username and password in a seperate TXT file but I figure if you are going to save that information why does it matter if it is in the root script or a seperate text file.

    Jorge R. Diaz

    Microsoft MVP | Office 365

    MCITP: Office 365 Administrator

    Check out my Blog

    Follow Me

    • Top 100 Contributor
    • Post Points: 0
  • Hi Jorge, Thanks for your reply. I have one more questions.

    In my case, each day I may have many new users will be created in AD and sync to O365. For each user, a attribute can be identify they can use o365 or not. My question is can I write script to automatically perform the following actions?

    1. Check any new user account are created in AD

    2. Check the attribute of the new user account to identify they have O365 license or not

    3. If they have O365 then assign license to them automatically.

    • Not Ranked
    • Post Points: 0
  • Sure, I haven't written one like that and it would take time to figure out but it could be done with the right CmdLets.

    Jorge R. Diaz

    Microsoft MVP | Office 365

    MCITP: Office 365 Administrator

    Check out my Blog

    Follow Me

    • Top 100 Contributor
    • Post Points: 0
  • Hi Poly,

    Can you assign license for Office 365 users bulkly with the script provided by Jorge? If the information is helpful, please show this thread as answered so others may benefit from the information. Thank you.

    Jack Sun

    • Top 50 Contributor
    • Post Points: 0
  • $AccountSkuId = "ourtennantedu:EXCHANGESTANDARD_STUDENT"

    $UsageLocation = "US"

    $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId

    $users = Get-MsolUser -UnlicensedUsersOnly -ALL -Domainname ourtennant.edu

    $Users | ForEach-Object {

    Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation $UsageLocation

    Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses $AccountSkuId -LicenseOptions

    $LicenseOptions

    }

    This worked great for me.  Note the $users param has = instead of |  

    and this only will return unlicensed users of a specific  domain.  which is handy if you have multiple sub domains.

    Thank you for the script!!

    • Not Ranked
    • Post Points: 0
    Suggested by
  • Hi

    I want to create a scheduled taks that runs daily that assigns licences to new users synced from on-premesie AD to O365. New users are not created in bulk in my organisation so i cant use a .csv wit a list of users as they are only created one a users completed their registration and their a/c is then created in our on-premesis AD then synced out.

    I need the script to do two things:

    1. set the usage location, this can be done by including the following in the script:

    Get-MsolUser -UnlicensedUsersOnly | Set-MsolUser -UsageLocation ie

    2. assign the licence, this can be done by including the following in the script:

    Set-MsolUserLicense -UnlicensedUsersOnly -AddLicenses "domain:STANDARDWOFFPACK_STUDENT"

    I need the script to automatically connect to the Windows Azure AD, then run the above commands. What combination of the above scripts can i use to connect to Windows Azure AD via my script?

    • Not Ranked
    • Post Points: 0
Page 1 of 1 (11 items)