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
- Click on the Admin Center Tile
- 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.
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
This command returns all the active users present in your O365 Account
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
- Expand Additional Details
- I have updated the User properties and now going to filter the Users based on “Job Title” property
Get-MsolUser -Title “SharePoint Technical Consultant”
This command export all the Users where “Job Title = SharePoint Technical Consultant”
Export All Users with matching filter and selected properties
Get-MsolUser -Title “SharePoint Technical Consultant” | Select DisplayName
This command export only “Display Names” for all the Users where “Job Title = SharePoint Technical Consultant”
Export All Users with matching filter and selected properties to a file
Get-MsolUser -Title “SharePoint Technical Consultant” | Select DisplayName > “C:\SharePoint-Online-User-Data.txt”
This command export only “Display Names” for all the Users where “Job Title = SharePoint Technical Consultant” to a Text File at the specified path
Create New Users
New-MsolUser -UserPrincipalName “firstname.lastname@example.org” -DisplayName “Prashant Bansal New User” -LastName “New User” -LicenseAssignment “spdevs001:DEVELOPERPACK” -UsageLocation “IN”
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.
Modify Existing Users
Set-MsolUser -UserPrincipalName “email@example.com” -LastName “Mohan Bansal” -DisplayName “Prashant Mohan Bansal”
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.
Once update is performed it might take a couple of minutes to take effect in O365 Account.
User Properties before update command executed:
User Properties after update command executed:
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.