Avatar of Jody Davis
Jody Davis
Flag for United States of America asked on

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!
PowershellExchangeMicrosoft 365

Avatar of undefined
Last Comment
Jose Gabriel Ortega Castro

8/22/2022 - Mon
Jose Gabriel Ortega Castro

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 Davis

ASKER
thank you. i'll try this out today.
Jody Davis

ASKER
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?
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Jose Gabriel Ortega Castro

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 Davis

ASKER
** 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.
ASKER CERTIFIED SOLUTION
FOX

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Jody Davis

ASKER
thanks man, that was great!
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
FOX

Nice
Jose Gabriel Ortega Castro

$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