How to Use Get-MsolUser To Create User Reports in Microsoft 365

  • Post author:
  • Post category:Main
  • Post last modified:August 7, 2023
  • Reading time:7 mins read

The Get-MsolUser cmdlet is part of the MSOnline PowerShell module used for managing your Microsoft 365 environment through a command line interface. Get-MsolUser can be used to detail specific information about a user in your organisation or to report on a group of users.

In this post I am going to show you how to use the cmdlet to get specific information about a user and also how to create useful reports about multiple users. It can also be incorporated into larger scripts to create an array or variable of users which can then be manipulated, such as by adding aliases to each mailbox or assigning Microsoft licenses to each user.

Pre-requisites

In order to use the Get-MsolUser cmdlet, you need to ensure you are running PowerShell version 6 or earlier and have the MSOnline module installed. If you do not have MSOnline PowerShell module installed or are receiving an error, follow my guide on How to Fix The Term ‘Connect-MsolService’ is Not Recognized

The MSOnline module is not currently support on PowerShell v7.

Using the Get-MsolUser cmdlet to get info on a specific user

If you are unsure of the user you wish to target, you can run the Get-MsolUser cmdlet alone and it will return a list of users in your organisation. From here you can identify the user you wish to target from the display name or user principal name.

get-msoluser example

Now you know the UserPrincipalName for the user you wish to target, you can use the following command to get basic information on that user:

get-msoluser -UserprincipalName [email protected]

You can see from the following output you are only returned basic information such as the User Principal Name, Display Name and whether the user is licensed:

Often you will be looking for more detailed information about the particular user. To find out more information we can add a Pipe ‘|’ with the format list parameter onto our command to return a list of all information on our user.

get-msoluser -UserprincipalName [email protected] | FL

You will now see returned, a long list of attributes and values for your selected user account. I wont paste the example here as it would be too long.

Instead of listing all information about your user by default, you can specific which information you are interested in by using the Select parameter. With the below command I have selected some useful information to gather about our user.

get-msoluser -UserprincipalName [email protected] |`
select DisplayName, UserPrincipalName, UsageLocation,`
UserType, WhenCreated

Here is what the output to our command looks like:

Lastly, in some instances you may want to export that information to a CSV for it to be manipulated or even uploading into another system. You can use the following command to do just that.

get-msoluser -UserprincipalName [email protected] |`
select DisplayName, UserPrincipalName, UsageLocation, UserType, WhenCreated |`
Export-CSV -NoTypeInformation -path C:\temp\userinfo2.csv

In the case of the above script, the csv file will be saved in C:\Temp\ with the file name userinfo2.csv.

csv output for get-msoluser

How to use Get-MsolUser to report on multiple users

To generate reports on multiple users in our organisations Microsoft 365 tenant using the Get-MsolUser command, we can build on what we have done above.

Here is a command we can use to list all users in our tenant and only list specific information about each user:

get-msoluser | select DisplayName, UserPrincipalName, UsageLocation, UserType, IsLicensed

Here is the output, as you can see there is a section for each user with the corresponding information we have selected:

Similar to above, we can also use the Export-CSV cmdlet to export this information into a CSV where it can be manipulated:

get-msoluser | select DisplayName, UserPrincipalName, UsageLocation, UserType, IsLicensed | `
Export-CSV -NoTypeInformation -path C:\temp\userinfo2.csv

This is what the output would look like after we have exported the information to a CSV:

csv output for get-msoluser multiple users

In the case we only want to target specific users we can adapt our command to search for specific attributes and specific words in attributes.

For example, if we want to list all users which do not have a usage location set, we can use the following script to search for users that have a null (or empty) usage location attribute:

get-msoluser | ? {$_.UsageLocation -ne $null} |`
select DisplayName, UserPrincipalName, UsageLocation, UserType, IsLicensed | `
Export-CSV -NoTypeInformation -path C:\temp\userinfo2.csv

You can see from the below, our CSV report now only lists users that do not have a usage location set.

csv output for get-msoluser null usage location

The above can be adapted to meet your needs. For example there are various operators you can use instead of -ne (Not Equals), such as:

  • -eq (equals exactly)
  • -like (Matches a wildcard patter)
  • -notlike (Does not match a wildcard pattern)
  • -gt (Greater than the specified value)
  • -ge (Greater or equal to the specified value)
  • -lt (Less than the specified value)
  • -le Less then or equal to the specified value)

You can also use Logical operators in your search queries, such as AND or OR. Here is another good example where we are finding all accounts without a usage location set with a display name that includes ‘Info’:

get-msoluser | ? {$_.UsageLocation -eq $null -and $_.DisplayName -like "Info"} |`
select DisplayName, UserPrincipalName, UsageLocation, UserType, IsLicensed | `
Export-CSV -NoTypeInformation -path C:\temp\userinfo2.csv

Leave a Reply