Deep Dive Into Office 365 PowerShell Cmdlets (Part 5)

by [Published on 28 July 2016 / Last Updated on 28 July 2016]

In the part 5, we will explain common Get-MsolUser PowerShell commands that you can use to retrieve user information from Office 365 Tenants that you generally can’t get by using the pre-defined parameters.

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

Introduction

In the part 4 of this article series, we explained various user properties and parameters that you can use with Get-MsolUser cmdlet to get a specific type of information for Office 365 users. For example, “–EnabledFilter” parameter provides you users that are enabled or disabled, “-ReturnDeletedUsers” returns the list of users that were deleted from Office 365, but are still present in the Office 365 Recycle bin, “-Synchronized” parameter allows you to collect the list of users that are synchronized from On-Premises Active Directory to Office 365 Tenant. While you can use pre-defined parameters with Get-MsolUser cmdlet to return specific type of information, but pre-defined parameters might not be useful when you need to collect information such as Office 365 plan and services assigned to the users, creation date and time of users, password expiry information and so on.

Common Get-MsolUser PowerShell Commands

Let’s take a look at some of the common Get-MsolUser PowerShell commands that you may find useful in your daily operational tasks when working with Office 365 users. Some of the common Office 365 user operational tasks that I can think of are collecting Office 365 user licensing information, collecting a list of Office 365 users based on their department, collecting user password expiry information and users that use a specific UsageLocation.

Collecting Office 365 Users Licensing Information

One of the tasks that Office 365 Administrators perform against an Office 365 subscription is to get licenses assigned to Office 365 users. Get-MsolUser cmdlet plays a vital role in collecting Office 365 licensing information. As stated in earlier parts of this article series, Office 365 stores licensing information in two user properties; IsLicensed and Licenses. “IsLicensed” is a single-valued property that stores only “True” or “False” data. In case you just need to know whether Office 365 users are assigned a license or not, you will run Get-MsolUser cmdlet with IsLicensed property as shown in the command below:

  • Get-MsolUser | select-Object UserPrincipalName, IsLicensed | Export-CSV C:\Temp\Office365UserLicenseStatus.CSV –NoTypeInformation

Licenses” is a multi-valued property which stores the Office 365 plans and services assigned to an Office 365 user. To see the data stored in “Licenses” property you need to use –ExpandProperty parameter with Get-MsolUser cmdlet as shown in the command below.

  • Get-MsolUser –UserPrincipalName JohnS@TechGenix.com | Select-Object –ExpandProperty Licenses

The above command returns the Office 365 Plans assigned to JohnS@TenixGenix.com Office 365 user as shown in the output below:

Image

Although by using the –ExpandProperty parameter with Get-MsolUser cmdlet you can know the Office 365 Plan assigned to the Office 365 user, but the service information is still not clear. By looking at the output above, you can know that the Office 365 user JohnS@TechGenix.com has been assigned ENTERPRISEPACK Office 365 plan, but the list of Office 365 services that this user is subscribed for is still not clear. It is because Office 365 stores Service data in another attribute called “ServiceStatus”. The “ServiceStatus” attribute contains service name, Office 365 plan and service status. So to get a list of services assigned to the user and the status of each service, you will execute below PowerShell commands:

  • $ServicesStatus = Get-MsolUser –UserPrincipalName JohnS@TechGenix.com | Select-Object –ExpandProperty Licenses
  • $ServicesStatus.Services

As you can see I stored the output of the Get-MsolUser command in a variable named $ServicesStatus and next command “$ServicesStatus.Services” lists services assigned to the Office 365 user and status of each service as shown in the screenshot below:

Image

In case you wish to get a count of Office 365 licenses assigned to Office 365 users, you can do so by executing the below PowerShell commands:

  • $LicenseCount = Get-MsolUser –All | Select-Object –ExpandProperty Licenses
  • $LicenseCount.Count

First command stores the Licenses for all Office 365 users in $LicenseCount variable and second command gets the count.

Password Expiry Information for Office 365 Users

You can use “PasswordNeverExpires” property to know whether users are forced to change their password or not after 90 days. “PasswordNeverExpires” property will return TRUE or FALSE. To export a list of users with PasswordNeverExpires attribute data, execute below command:

  • Get-MsolUser | select-Object UserPrincipalName, PasswordNeverExpires | Export-CSV C:\Temp\PassNeverExpiresUsers.CSV –NoTypeInformation

Tip:
“WhenCreated” property applies to both types of users; users created in Office 365 Cloud and users synced from On-Premises Active Directory.

Getting Creation Date and Time of Office 365 Users

For troubleshooting purposes, you can collect other information such as getting creation date and time of an Office 365 user. To get the creation time of an Office 365 user, you can use “WhenCreated” property with Get-MsolUser cmdlet as shown in the command below.

  • Get-MsolUser -UserPrincipalName Username@DomainName | select-Object UserPrincipalName, WhenCreated

And the result returned from the above command is shown in the screenshot below:

Image

The above command returns “WhenCreated” property value for a single user. If you would like to see “WhenCreated” value for all users, execute below PowerShell command:

  • Get-MsolUser –All | select-Object UserPrincipalName, WhenCreated

And to export the list to a CSV file, add “Export-CSV” cmdlet as shown in below command:

  • Get-MsolUser –All | select-Object UserPrincipalName, WhenCreated | Export-CSV C:\Temp\UsersWithWhenCreated.CSV -NoTypeInformation

As stated earlier, Get-MsolUser cmdlet supports a number of user properties. If you wanted to get a list of users based on their department, you can do so by using the “Department” attribute as shown in the command below:

  • Get-MsolUser –All | Where { $_.Department –eq “Finance” } | Select-Object UserPrinicipalName, DisplayName, Department | Export-CSV C:\Temp\FinanceOffice365Users.CSV –NoTypeInformation

Wrapping it up...

In this part, we explained some Get-MsolUser commands such as knowing when a user was created in the Office 365 Tenant, user licensing information and querying Office 365 Tenant to return a list of users based on their department.

In the next part, we will continue to explain more Get-MsolUser commands that you might find useful in your daily operational tasks for an Office 365 Tenant.

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