Office 365 - User Management Using PowerShell

If you want to follow along, then the prerequisites for this article are given below.

  • Having an O365 Account created.
  • PowerShell for Office 365 configured.

If the above prerequisites do not meet the criteria, 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 the prerequisites.

  • Login to O365 portal.

    1
  • Click Admin Center Tile.

    2
  • Click 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 trial 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 sections given below, 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, which are 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 added user. Based on these properties, we can export a filtered set of the users. Let's 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, I am going to filter the Users, which are based on Job Title property.

    7
Command

Get-MsolUser -Title “SharePoint Technical Consultant”.

Usage

This command exports 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 exports only displays names for all the users, where “Job Title = SharePoint Technical Consultant”.

Output

9

Export all the 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 displays 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 “[email protected]” -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 and this is used by Microsoft to track the location, where the Service is consumed. This valid value for this parameter can be a two letter country code -- for India, it should be “IN”.

If I execute this command, I would get the error, as shown below since a free trial O365 account grants me a Single User license only.

If I have a 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 “[email protected]” -LastName “Mohan Bansal” -DisplayName “Prashant Mohan Bansal”

Usage

By using this command, we can modify an existing user in our O365 account, which is specified by the parameter “UserPrincipalName” and provides its properties like Display Name, Last Name and so on, which needs to be updated.

Output

12

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

User Properties are given below, before the update command is executed.

13

User Properties are given after update command is executed.

14

15

All the above operations can be automated and using same PowerShell development paradigms, which we followed for On Premise SharePoint implementation.

I hope you found it helpful.

Up Next
    Ebook Download
    View all
    Learn
    View all