Office 365: User Management using Powershell

In this article we will discuss some of the useful PowerShell Commands for managing Users in Office 365 (O365)

If you want to follow along then the prerequisite for this article are:

  • Having a O365 Account created
  • PowerShell for Office 365 configured

If the above prerequisites does not meet then I would recommend you to read one of my earlier article “Office 365: How to Configure PowerShell for O365” to get to know the steps of installing prerequisites.

  • Login to O365 Portal

1

  • Click on the Admin Center Tile

2

  • Click on “Active Users” navigation link under “Users” in the left navigation

Currently we can see only one Active User “Prashant Bansal”.

Since this is a free trail account so we cannot have more than one user here, but if you go for a paid plan then you can get multiple users as well.

3

Now in the following sections we will be looking into some of the useful operation and corresponding PowerShell commands regarding User Management

Export All Users

Command

Get-MsolUser

Usage

This command returns all the active users present in your O365 Account

Output

4

5

Export All Users with matching filter

Let’s see some of the properties of the User Profile available for every User added. Based on these properties we can export filtered set of Users. Lets’ check for the User properties first –

  • Select the User
  • Click Edit on the Profile Card on the right

6

  • Expand Additional Details
  • I have updated the User properties and now going to filter the Users based on “Job Title” property

7

Command

Get-MsolUser -Title “SharePoint Technical Consultant”

Usage

This command export all the Users where “Job Title = SharePoint Technical Consultant”

Output

8

 Export All Users with matching filter and selected properties

Command

Get-MsolUser -Title “SharePoint Technical Consultant” | Select DisplayName

Usage

This command export only “Display Names” for all the Users where “Job Title = SharePoint Technical Consultant”

Output:

9

Export All Users with matching filter and selected properties to a file

Command

Get-MsolUser -Title “SharePoint Technical Consultant” | Select DisplayName > “C:\SharePoint-Online-User-Data.txt”

Usage

This command export only “Display Names” for all the Users where “Job Title = SharePoint Technical Consultant” to a Text File at the specified path

Output

10

Create New Users

Command

New-MsolUser -UserPrincipalName “spdev002@spdevs001.onmicrosoft.com” -DisplayName “Prashant Bansal New User” -LastName “New User” -LicenseAssignment “spdevs001:DEVELOPERPACK” -UsageLocation “IN”

Usage

This command Adds a new User to the O365 Account specified by parameter “UserPrincipalName” and sets its basic properties like “Display Name, Last Name” and so on. It also assigns the license to the user specified by the parameter “LicenseAssignment”.

It is important to note that passing location information as specified by the parameter “UsageLocation” is mandatory as this is used by Microsoft for tracking the location where the service is consumed. This valid value for this parameter could be a two letter country code like for India it should be “IN”

If I execute this command I would get the following error since a free trail O365 account grant me a Single User license only.

If I am having proper license supporting multiple Users, this command would add a new User to my O365 Account.

Output

11

Modify Existing Users

Command

Set-MsolUser -UserPrincipalName “spdev001@spdevs001.onmicrosoft.com” -LastName “Mohan Bansal” -DisplayName “Prashant Mohan Bansal”

Usage

Using this command we can modify an existing User in our O365 Account specified by parameter “UserPrincipalName” and providing its properties like “Display Name, Last Name” and so on that needs to be updated.

Output

12

Once update is performed it might take a couple of minutes to take effect in O365 Account.

User Properties before update command executed:

13

User Properties after update command executed:

14

15

That all for this article on User Management using PowerShell for O365.

All of the above Operations can be automated and using same PowerShell Development paradigms as we followed for On Premise SharePoint Implementations.

Hope you find it helpful.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s