none
Script to assign licenses to bulk AD synced user’s office 365 RRS feed

  • Question

  • i have dirsync so all my local ad users are synced with office 365 tenant

    Now I am migrating mailboxes and I need to assign licenses to a bulk migrated users using csv

    This process requires 2 steps ‘cmdlts’

    $usagelocation = “SA”

    Set-MsolUser –UserPrincipalName user@domain -UsageLocation $UsageLocation

    After defining the location above time to assign the licenses

     Set-MsolUserLicense –UserPrincipalName user@domain  -AddLicenses company:ENTERPRISEPACK

    So how can I script this and use a CSV file?

    Also strange thing, I have ran the above to about 13 users manually, I placed the command on a notepad

    Copied all lines and past on the PS, after that other users who already had licenses had their licenses removed! So what did I do wrong? I am asking this to make sure the script you will provide will not remove the currently assigned licenses


    • Edited by MAHER0 Tuesday, May 20, 2014 1:53 PM
    Tuesday, May 20, 2014 1:29 PM

Answers

  • this script will work for you given the below conditions

    1- you have csv file with only one Column named "UserPrincipalName" and has all users need to be licensed

    2- all users in the above file in the format of username@yourdomain.com

    3- All users are having the same license

    $path= Import-Csv -Path "usersFiles.csv"
    foreach ($item in $path){
    $MSOLUserName= $item.UserPrincipalName
    $password = ConvertTo-SecureString "password here" -AsPlainText –Force
    $credential = New-Object System.Management.Automation.PsCredential("your username here",$password)
    $cred = Get-Credential -cred $credential
    Import-Module MSOnline
    Connect-Msolservice -cred $cred
    $AccountSkuId = "tenantdomain:ENTERPRISEPACK" 
    $UsageLocation = "SA" 
    $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId 
    Set-MsolUser -UserPrincipalName $MSOLUserName -UsageLocation $UsageLocation
    Set-MsolUserLicense -UserPrincipalName $MSOLUserName -AddLicenses $AccountSkuId  -LicenseOptions $LicenseOptions
    }
    
    


    • Edited by Taheito Tuesday, May 20, 2014 3:09 PM
    • Marked as answer by MAHER0 Wednesday, May 28, 2014 6:50 AM
    Tuesday, May 20, 2014 3:07 PM

All replies

  • This is untested, use at your own risk. This isn't an O365 forum, it's for general PowerShell inquiries.

    Import-Csv .\userList.csv | ForEach {
    
        Set-MsolUser -UserPrincipalName $_.UPN -UsageLocation $_.Location
    
        Set-MsolUserLicense -UserPrincipalName $_.UPN -AddLicenses snipped:ENTERPRISEPACK
    
    }

    This will require an input CSV with one column named UPN that contains the UPN info and a second column named Location with the location info.


    EDIT: Post has been updated.

    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Tuesday, May 20, 2014 1:39 PM
    Moderator
  • sorry i mistakenly added a domain name in my question

    please remove it from your answer

    also i am not good on scripting, just to confirm i need to copy this and save it as a PS1 then run it

    not copy and past on the PS window

    what should the CV file look like ?


    • Edited by MAHER0 Tuesday, May 20, 2014 1:54 PM
    Tuesday, May 20, 2014 1:50 PM
  • Hello Maher, 

    I have my own script which assigning the licenses for m users automatically(250K)

    to custom that script for you, you have to give us the license pack you have

    as you know in WAVE15 there are new roles, so if you have Office365 for education then you have two categories one for students and one for faculties

    so can you take a screenshot of the license you have ?

    Tuesday, May 20, 2014 2:10 PM
  • its enterprise e3
    Tuesday, May 20, 2014 2:32 PM
  • this script will work for you given the below conditions

    1- you have csv file with only one Column named "UserPrincipalName" and has all users need to be licensed

    2- all users in the above file in the format of username@yourdomain.com

    3- All users are having the same license

    $path= Import-Csv -Path "usersFiles.csv"
    foreach ($item in $path){
    $MSOLUserName= $item.UserPrincipalName
    $password = ConvertTo-SecureString "password here" -AsPlainText –Force
    $credential = New-Object System.Management.Automation.PsCredential("your username here",$password)
    $cred = Get-Credential -cred $credential
    Import-Module MSOnline
    Connect-Msolservice -cred $cred
    $AccountSkuId = "tenantdomain:ENTERPRISEPACK" 
    $UsageLocation = "SA" 
    $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId 
    Set-MsolUser -UserPrincipalName $MSOLUserName -UsageLocation $UsageLocation
    Set-MsolUserLicense -UserPrincipalName $MSOLUserName -AddLicenses $AccountSkuId  -LicenseOptions $LicenseOptions
    }
    
    


    • Edited by Taheito Tuesday, May 20, 2014 3:09 PM
    • Marked as answer by MAHER0 Wednesday, May 28, 2014 6:50 AM
    Tuesday, May 20, 2014 3:07 PM
  • if the above script did not work for please run the below command and send me the result

    Get-MsolAccountSku

    Tuesday, May 20, 2014 3:11 PM
  • also you can use the below script to get ALL unlicensed user on Office365 and license them

    $password = ConvertTo-SecureString "password here" -AsPlainText –Force
    $credential = New-Object System.Management.Automation.PsCredential("your username here",$password)
    $cred = Get-Credential -cred $credential
    Import-Module MSOnline
    Connect-Msolservice -cred $cred
    $AllO365 = Get-MsolUser -all  -DomainName myDomain.com -UnlicensedUsersOnly 
    $AccountSkuId = "tenantdomain:ENTERPRISEPACK" 
    $UsageLocation = "SA" 
    $LicenseOptions = New-MsolLicenseOptions -AccountSkuId $AccountSkuId 
    $AllO365|ForEach-Object{Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation $UsageLocation
    Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName  -AddLicenses $AccountSkuId  -LicenseOptions $LicenseOptions}


    • Edited by Taheito Tuesday, May 20, 2014 3:58 PM correct the code
    Tuesday, May 20, 2014 3:29 PM
  • Tuesday, May 20, 2014 6:26 PM
  • many thanks i am out now i will try this tomorrow and let you know
    Tuesday, May 20, 2014 6:27 PM
  • got it Mike Laughlin
    i will

    Cheers. I just re-read my update and I didn't intend for it to sound as harsh as it came out. I've readjusted the post.

    Let us know if you run into any problems.


    Don't retire TechNet! - (Don't give up yet - 12,830+ strong and growing)

    Tuesday, May 20, 2014 6:41 PM
    Moderator
  • you tried it ?
    Wednesday, May 21, 2014 3:48 PM
  • yes i did and it works perfectly

    thank you

    Wednesday, May 28, 2014 6:50 AM
  • it is ok Mike , thank you 

    Wednesday, May 28, 2014 6:51 AM