Adding Bulk Users to Office 365 via PowerShell

PowerShell is so powerful and makes tasks very easy to do, as long as you know the syntax. What I like to do is keep OneNote with a library of commonly used PowerShell commands that I can call upon as I need them. I will eventually share that OneNote. For this post I want to cover the PowerShell version of what I covered on Sunday March 3, 2016 which is adding Bulk Users into Office 365 via PowerShell instead of the GUI.

Before jumping into this, keep in mind that there are a few prerequisites.

  1. You will need the Microsoft Online Services Sign-In Assistant ( https://www.microsoft.com/en-ca/download/confirmation.aspx?id=39267)
  2. You will need the Azure Active Directory Modules (http://go.microsoft.com/fwlink/p/?linkid=236297)

Once this is done, you are ready to connect to Office 365 via PowerShell. There are a few preparation steps we will need to accomplish before we dive into Office 365.

  1. Log into the portal and go to the Admin section.
  2. Go to Users > Active Users and click Bulk Add (Bulk Add.)
  3. Here you have two options, you can download the Blank CSV or Download the Sample CSV file
  4. Edit and save the CSV file with the users you want to import into Office 365

Tip: I like the use the Sample CSV file as it shows you what is expected in every field (Only Display Name and User Name are required). Mind you only the first few fields are required, but the format for the username can easily throw people off as it’s somewhat different than a username in Active Directory. It is expecting email address format.

Now to Import the users.

  • Open the Windows Azure Active Directory Module for Windows PowerShell
  • type connect-msolservice (this is the command line that will establish the connection to Office 365/Azure Active Directory)

Connect-MSOLService

  • Enter your Adminstrative credentials for your Office 365 tenant

Enter Credentials

Now that you’re connected to Office 365. It’s time to import your users.

I had to change the CSV file around to fit what the PowerShell Command is expecting. First off let’s look at the cmdlet.

Import-Csv -Path “C:\Users\bryan\Documents\Blogs\usersviaps.csv” | foreach {New-MsolUser -DisplayName $_.DisplayName -FirstName $_.FirstName -LastName $_.LastName -UserPrincipalName $_.UserPrincipalName -UsageLocation $_.UsageLocation -LicenseAssignment $_.AccountSkuId} | Export-Csv -Path
C:\Users\bryan\Documents\Blogs\NewAccountResults.csv

Tip: Change the parts in bold to match your requirements.

Notice the import cmdlet is looking for specific fields in a specific order. DisplayName, FirstName, LastName, UserPrincipalName and UsageLocation

I created my CSV file to mirror this format.

CSV to import

Once the cmdlet is finished, it will not return a “successful message” many cmdlets don’t but if you don’t see a big red error message and the shell simply returns you to a command line, typically you’re in good shape.

cmdlet

You will notice in our cmdlet, we piped the results to export the CSV. In this CSV, we will find something very familiar. It looks a lot like the import form that we downloaded earlier, except this one contains our newly created users passwords.

If you log into your portal you will notice that your newly imported users are now available for use. Awesome!

Newly Created Users

 

 

 

 

 

 

Leave a comment

Your email address will not be published.


*