Deep Dive Into Office 365 PowerShell Cmdlets (Part 3)

by [Published on 17 May 2016 / Last Updated on 17 May 2016]

In this and subsequent parts of this article series, we will learn how to interact with Office 365 objects such as users, groups, domains, services by using Office 365 PowerShell cmdlets.

If you would like to read the other parts in this article series please go to:

Introduction

In the first part of this article series, we gave an overview of Office 365 PowerShell cmdlets, and steps for installing ‘Windows Azure Active Directory Module for Windows PowerShell”. Part II explained the various PowerShell cmdlets that are available to manage different aspects of an Office 365 tenant.

Before you can run any Office 365 PowerShell cmdlets, you will be required to connect to an Office 365 subscription. As stated in the Part II of this series, you can use Connect-MsolService PowerShell cmdlet. Once you have connected to an Office 365 Subscription, use PowerShell cmdlets explained throughout the rest of this series to manage users, groups, domains, and to perform other common tasks for an Office 365 tenant.

Managing Users in Office 365

There are several PowerShell cmdlets that get installed when you install Azure Active Directory for Windows PowerShell, including the PowerShell cmdlets that you can use to manage Office 365 users. Although Office 365 Admin Center provides a separate section to work with the Office 365 users, PowerShell cmdlets offer you greater flexibility. Before we start using Office 365 user cmdlets, it is important to note that there are a few user cmdlets that you might not find useful, but there are many other cmdlets that help you save a lot of time in your daily operational tasks. Let us use some of the user PowerShell cmdlets that you can use for daily operational tasks.

Get Office 365 Users

“Get-MsolUser” is the first PowerShell cmdlet that I will be explaining in this article. Get-MsolUser cmdlet is very powerful. It supports a number of properties. As the name suggests, by running the cmdlet with no parameters gets you the list of users created in an Office 365 tenant as shown in the screenshot below:

Image
Figure 1

If you wanted to export the list in a CSV file for later use, you can run below PowerShell cmdlet:

  • Get-MsolUser | Export-CSV AllMsolUsers.CSV -NoTypeInformation

Get-MsolUser cmdlet also supports getting information for a particular Office 365 user as shown in the command below:

  • Get-MsolUser –UserPrincipalName User778@ITDynamicPacks.Net

It is important to understand that you might have several users created in the Office 365. If you have a large number of users and when running Get-MsolUser cmdlet, it will display you a warning message as shown in the screenshot below:

Image
Figure 2

By default, Get-MsolUser cmdlet can display a list of 500 users in one session. It doesn’t show all users. All of the Office 365 cmdlets have been designed to return only 500 objects in one session. In case you wish to see more than 500 users, you need to use either –MaxResults or –ALL parameter as indicated in the warning message shown in the screenshot above.

Note:
Since I have more than 500 users created in my trial Office 365 subscription, you can see me using either “–ALL” or “-MaxResults” parameter in all of the PowerShell cmdlets that have been explained throughout this article series.

Get-MSOlUser cmdlet and Properties

Get-MSolUser cmdlet supports many properties. In case you need to see the list of properties supported by the Get-MSolUser cmdlet, run below command:

  • Get-MSolUser –MaxResults 1 | Get-Member

Tip:
You don’t need to use the “–MaxResults” parameter in the above command unless you have more than 500 users created in the Office 365 tenant.

The above command returns the list of properties that you can use to retrieve information about Office 365 users as shown in the screenshot below:

Image
Figure 3

One of the properties I always use is “IsLicensed” property. “IsLicensed” property helps you know whether a user or a set of users are using an Office 365 license. Every user in Office 365 needs to be assigned with a license before the user can use Office 365 services such as email, SharePoint, or Lync or even be able to download Office 365 applications such as Word or excel or an application.

To get a list of users that have been assigned licenses in Office 365, run the below PowerShell command:

  • Get-MsolUser –ALL | Where-Object {$_.IsLicensed –eq “TRUE”}

 And if you wish to export the output to a CSV file, use the below command:

  • Get-MsolUser -ALL | Where-Object {$_.IsLicensed -eq "TRUE"} | select-object Name, DisplayName, UserPrincipalName, IsLicensed | Export-CSV C:\Temp\Office365LicensedUsers.CSV -NoTypeInformation

In case you need a list of non-licensed users, replace “TRUE” with “FALSE” in above commands or use “-UnlicensedUsersOnly” parameter with Get-MsolUser cmdlet as explained in below commands:

To list unlicensed users by specifying “FALSE” to IsLicensed property:

  • Get-MsolUser –ALL | Where-Object {$_.IsLicensed –eq “FALSE”}

To export unlicensed users to a CSV file by specifying “FALSE” to IsLicensed property:

  • Get-MsolUser -ALL | Where-Object {$_.IsLicensed -eq "FALSE"} | select-object Name, DisplayName, UserPrincipalName, IsLicensed | Export-CSV C:\Temp\Office365NonLicensedUsers.CSV –NoTypeInformation

And to export unlicensed users to a CSV file by specifying “-UnlicensedUsersOnly” parameter:

  • Get-Msoluser –All –UnLicensedUsersOnly | | Export-CSV C:\Temp\Office365NonLicensedUsers.CSV –NoTypeInformation

Note that the above one-liner commands are helpful when you are working in a PowerShell window, but they are not useful when you need to take an action as part of a script. For example, you might want to assign an Office 365 license based on the country of a user. Similarly, you might want to take an action only if a user is licensed. In these situations, you need to use “ForEach” loop in a PowerShell script that will help you check the condition before taking any action. For example, the below PowerShell code can be used to take an action if the Office 365 user is “not” licensed.

  • $MyOffice365Users = Get-MsolUser
  • ForEach ($AllUsers in $MyOffice365Users)
  • {
  •    $UserStat = $AllUsers.IsLicensed
  •    IF ($UserStat -eq "FALSE")
  •    {
  •      [ Insert PowerShell Commands to be executed here as part of the action ]
  •    }
  • }

Summary

We explained a little about Get-Msoluser cmdlet in this article. In this next part of this article series, we will continue to explain how to retrieve other properties of Office 365 users such as Multi-Factor Authentication status, user personal details, Last Password change, Password policies applied to users, and many more.

We will also learn how to create an Office 365 License report using Get-MsolUser cmdlet.

If you would like to read the other parts in this article series please go to:

See Also


The Author — Nirmal Sharma

Nirmal Sharma avatar

Nirmal Sharma is a MCSEx3, MCITP, and was awarded Microsoft MVP in Directory Services. In his spare time, he likes to help others and share some of his knowledge by writing tips and articles for various online communities. Nirmal can also be found contributing to PowerShell based Dynamic Packs for ADHealthProf.ITDynamicPacks.Net solutions.

Advertisement

Featured Links