Need Powershell command to bulk assign E3 licenses to newly-created O365 mailboxes

I have a .csv file of users i need to assign O365 E3 licenses. I need a powershell command to bulk assign the licenses using the .csv. I have a hybrid Exchange Server 2013 onprem server and our mailboxes are in O365 in the cloud.  Thanks!
Jody DavisSystems EngineerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
It's not that simple.

1. Connecto to MsolService
   a. Open a powershell console as administrator
   b. Run

install-module msonline

Open in new window


  c. run and add the credentials:
Connect-MsolService

Open in new window


2. Find all users that doens't have licences assigned:
  a. Run:
   Get-MsolUser -All -UnlicensedUsersOnly

Open in new window


3. you can only assign license to those users that DOES have a location:
 a. You can find all the users that doens't have assigned a location:
 
Get-MsolUser -All | where {$_.UsageLocation -eq $null}

Open in new window

b. Set location like this:
Set-MsolUser -UserPrincipalName "<Account>" -UsageLocation <CountryCode>
ex: et-MsolUser -UserPrincipalName "belindan@litwareinc.com" -UsageLocation US

Open in new window

 
If you use the command without the -All you'll get the 1st 500.
4. Find MsolAccountSku
a. run
Get-MsolAccountSku

Open in new window


Find the SKU of your ENTERPRISEPACK.
5. Assign Licenses:
a. Single user
Set-MsolUserLicense -UserPrincipalName "<Account>" -AddLicenses "<AccountSkuId>"

Open in new window

Office 365 Enterprise E3 (<domain>:ENTERPRISEPACK)
b. Massive Asignement:
$x = Get-MsolUser -All -UnlicensedUsersOnly [<FilterableAttributes>]; $x | foreach {Set-MsolUserLicense -AddLicenses "<AccountSkuId>"}

Open in new window



-------------------

$CSVPath="C:\here.csv"
Install-Module msonline
Connect-Msolservice
#Read csv
$objs = Import-Csv -Path "$CSVPath"
$SKUID= Get-MsolAccountSku | where { $_.Accountskuid -like "*ENTERPRISEPACK"} | select -ExpandProperty AccountSkuid
$objs | foreach {Set-MsolUserLicense -AddLicenses  $SKUID}

Open in new window

Jody DavisSystems EngineerAuthor Commented:
thank you. i'll try this out today.
Jody DavisSystems EngineerAuthor Commented:
Am I correct in saying I need to:
1. ensure that all users I want to run the script on have their location set
2. have a .csv with one column that has jsmith@constoso.com, jdoe@contoso.com, etc of the accounts I want to license
3. input the result of the ENTERPRISEPACK query into your script
4. run the script

That will do it, and this will license only those users in the .csv. Is that all correct?
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
Yes, last correction would be that I would need to know the column name for that UPN (joe@contoso.com), assuming that is "Column name" the final line would be

$objs | foreach {Set-MsolUserLicense $_."Column name" -AddLicenses  $SKUID}

Open in new window

Jody DavisSystems EngineerAuthor Commented:
** last request - how can i first bulk assign the location only to people in that .csv? (don't want to do it to all objects)

you've been great.
FOXActive Directory/Exchange EngineerCommented:
Jody

Connect to o365 via powershell

Find the SKU of your ENTERPRISEPACK   example -  domain:ENTERPRISEPACK_GOV
1. Create a folder named temp on your c:\drive
2. Have your .csv contain a header named UserPrincipalName and list the user's Userprincipalnames under that same header
3. Place your .csv file in the temp folder, let's name it Users.csv   example c:\temp\users.csv
4 Run the following

Import-Csv 'c:\temp\Users.csv'| %{Set-MsolUser -UserPrincipalName $_.UserPrincipalName -UsageLocation US}

Import-Csv 'c:\temp\Users.csv'| %{Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses 'domain:ENTERPRISEPACK_GOV'}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jody DavisSystems EngineerAuthor Commented:
thanks man, that was great!
FOXActive Directory/Exchange EngineerCommented:
Nice
Jose Gabriel Ortega CastroCEO Faru Bonon IT - EE Solution ExpertCommented:
$CSVPath="C:\here.csv"
#Install-Module msonline copmmented becasue i assume that is already installed
Connect-Msolservice
#GET SKUID
$SKUID= Get-MsolAccountSku | where { $_.Accountskuid -like "*ENTERPRISEPACK"} | select -ExpandProperty AccountSkuid

#read and set location
Import-Csv -Path "$CSVPath" | Set-MsolUser $_.UserPrincipalName -UserPrincipalName -UsageLocation US
#read and set SKUID license
Import-Csv -Path "$CSVPath" | Set-MsolUserLicense -identity $_.UserPrincipalName  -AddLicenses  $SKUID

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.