How to manage users in Office 365 with PowerShell – Part 2

In the previous post How to manage users in Office 365 with PowerShell – Part 1 there was an introduction on how to use popular cmd-lets to essentially manipulate the details about Cloud Identities including their permanent deletion from the Recycle Bin in the Azure Active Directory.

So creating Office 365 users using the PowerShell command it is a very easy process. But what if we want to repeat the same actions for multiple users at the same time? The purpose of this post is instead to show the steps for importing multiple users in bulk mode.

Importing the users in bulk mode it’s an easy process and can be easily done by first creating a sample CSV file holding at least these Columns:

  • UserPrincipalName (Mandatory field)

  • DisplayName

  • FirstName

  • LastName

  • Password

  • Department

  • City

Let’s create a CSV file called SampleUsers.csv. Our file will look something like this:

At this point we can leverage the Import-CSV command in PowerShell to read the details from this file and use them to create new users in Ofice 365. In this instance we can select for example to show the UserPrincipalName,DisplayName, Department and City

This screenshot also shows the values have been imported successfully. To make things even easier let’s assign this command to a variable like in the example below:

$MyUsers = Import-Csv C:\Users\Michele\Desktop\SampleUsers.csv |select UserPrincipalName,DisplayName,Department,City

 And now with a one line command we can import all users in bulk:

forEach ($MyUsers in $MyUsers) {New-MsolUser -UserPrincipalName$MyUsers.UserPrincipalName -DisplayName $MyUsers.DisplayName -FirstName $MyUsers.FirstName -LastName $MyUsers.LastName -Password $MyUsers.Password -Department $MyUsers.Department -City$MyUsers.City}

By executing again a Get-MsolUser will show the newly created users in Office 365

And what if we also want to license these user during import? We have teo options here:

 Option 1

Modify the CSV file to include also the SubscriptionID and then add the pertinent parameter like the previous example

-LicenseAssignment $MyUsers.LicenseAssignment

 Option 2

Use the command Get-MSolAccountSku and associate the AccountSkuID value. To do a bulk update of the user licenses the steps will be:

  1. Create a variable like $Sku = Get-MsolAccountSku

  2. Run the command forEach ($MyUsers in $MyUsers) {Set-MsolUser -UserPrincipalName $MyUsers.UserPrincipalName -Addlicenses$ku.AccountSkuID

 This concludes the information about importing users in Office 365. There is still another method to import users on Office 365. This is with Azure Active Directory Connect. But rather simple importing I prefer to talk about Syncing Users to Office 365 which certailnly offers much more options and configurations. This and much more will be covered in my next posts.

References:

Import-CSV

Get-MsolAccountSku

New-MsolUser

Set-MsolUser

Michele Domanico

Passionate about Virtualization, Storage, Data Availability and Software Defined Data Center technologies. The aim of Domalab.com is sharing with the Community the knowledge and experience gained with customers, industry leaders and like minded peers. Always open to constructive feedback and new challenges.

Leave a Reply

%d bloggers like this: