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

Jody Davis
Jody Davis used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

Commented:
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 Engineer

Author

Commented:
thank you. i'll try this out today.
Jody DavisSystems Engineer

Author

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?
Ensure you’re charging the right price for your IT

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

Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

Commented:
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 Engineer

Author

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.
Active Directory/Exchange Engineer
Top Expert 2015
Commented:
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'}
Jody DavisSystems Engineer

Author

Commented:
thanks man, that was great!
FOXActive Directory/Exchange Engineer
Top Expert 2015

Commented:
Nice
Jose Gabriel Ortega CastroTop Rated Freelancer on MS Technologies
Awarded 2018
Distinguished Expert 2018

Commented:
$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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial