Home » Office 365 » Get-MsolUser Filter With Examples

Get-MsolUser Filter With Examples

The Get-MsolUser cmdlet in PowerShell retrieves information about user accounts in Microsoft Office 365.

You can use filters with the Get-MsolUser cmdlet to specify which user accounts you want to retrieve based on specific criteria. The filter parameter helps you narrow down your query to target specific users or groups of users.

In this article, we will discuss how to use filters with the Get-MsolUser cmdlet to retrieve user information based on search criteria.

How to Filter Msol User by a UserPrincipalName

To filter Msol User by a UserPrincipalName (UPN), you can run the following command.

Get-MsolUser -UserPrincipalName "[email protected]"

This command retrieves the information about the user with the user principal name (UPN) “[email protected]“.

The output of the above PowerShell script to filter a user by UPN is given below.

PS C:\> Get-MsolUser -UserPrincipalName "[email protected]" 
                                            
UserPrincipalName                 DisplayName isLicensed
-----------------                 ----------- ----------
[email protected] john alwin  True

How to Filter Users by Multiple Criteria Using Logical Operators

To filter users by multiple criteria using logical operators such as “AND” and “OR“, you can run the following command.

Get-MsolUser | Where-Object {$_.Department -eq "Finance" -and $_.Country -eq "United States"} 

In the above PowerShell script, the Get-MsolUser command retrieves users from the Azure Active Directory and pipes them to the Where-Object cmdlet.

The Where-Object command checks users belong to the “Finance” department and are in the “United States” country.

The output of the above PowerShell script to retrieve Office 365 users filtered by multiple criteria is given below.

Get-MsolUser Filter by logical operator
Get-MsolUser Filter by a logical operator

How to Filter Users with Specific Licenses in Office 365

To filter users with specific licenses in Office 365, run the following command.

Get-MsolUser -All | Where-Object {($_.licenses).AccountSkuId -match "ENTERPRISEPACK"}

This command retrieves the user accounts in Office 365 with a specific license “ENTERPRISEPACK“.

The output of the above PowerShell script to filter based on the attributes and criteria relevant to your Office 365 user accounts is given below.

PS C:\> Get-MsolUser -All | Where-Object {($_.licenses).AccountSkuId -match "ENTERPRISEPACK"}                           
UserPrincipalName                  DisplayName       isLicensed
-----------------                  -----------       ----------
[email protected] shell geek True
[email protected] samer amberson    True
[email protected]  john alwin        True

How to Get Enabled User Accounts in Office 365

To get enabled user accounts in Office 365, run the following command.

 Get-MsolUser -EnabledFilter EnabledOnly -MaxResults 2000 

This command retrieves the enabled user accounts in your Office 365. The Get-MsolUser cmdlet uses the EnabledFilter parameter to filter based on the EnabledOnly criteria and returns the result.

The output of the above PowerShell script to retrieve enabled user accounts in Office 365 is given below.

PS C:\> Get-MsolUser -EnabledFilter EnabledOnly -MaxResults 2000                                                        
UserPrincipalName                  DisplayName       isLicensed
-----------------                  -----------       ----------
[email protected] shell geek True
[email protected] samer amberson    True
[email protected]  john alwin        True
[email protected]  gary edler        False

How to Filter User Accounts by Specific Attribute

To filter user accounts by specific attributes such as DisplayName, run the following command.

Get-MsolUser | Where-Object { $_.DisplayName -like "*john*" }

In the PowerShell script, the Get-MsolUser command gets users in the Azure AD and pipes them to the Where-Object cmdlet.

The Where-Object command uses the criteria to filter user accounts where DisplayName is like “*john*”.

The output of the above PowerShell script retrieves the user account in Office 365 where the user displayname like “*john*”.

PS C:\> Get-MsolUser | Where-Object { $_.DisplayName -like "*john*" }                                                   
UserPrincipalName                 DisplayName isLicensed
-----------------                 ----------- ----------
[email protected] john alwin  True

How to Filter User Account by SearchString in Office 365

To filter user accounts by SearchString in Office 365, run the following command.

Get-MsolUser -SearchString joh

In the above PowerShell script, the Get-MsolUser cmdlet uses the SearchString parameter to search for the user accounts in Office 365 that have “joh” in their display name or email address.

The output of the above PowerShell script is given below.

PS C:\> Get-MsolUser -SearchString joh                                                                                  
UserPrincipalName                 DisplayName isLicensed
-----------------                 ----------- ----------
[email protected] john alwin  True

Cool Tip: How to use the Set-MsolUser cmdlet in PowerShell!

Conclusion

I hope the above article on how to filter the user accounts in Office 365 based on multiple criteria is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.