The Get-MsolGroup cmdlet in PowerShell gets groups from Microsoft Office 365. This command returns a single group if you specify the ObjectId parameter, or search within all groups in Azure Active Directory.
The syntax to get a list of groups from Azure Active Directory with PowerShell is given below.
Get-MsolGroup
[-UserObjectId <Guid>]
-ObjectId <Guid>
[-IsAgentRole]
[-UserPrincipalName <String>]
[-GroupType <GroupType>]
[-HasErrorsOnly]
[-HasLicenseErrorsOnly <Boolean>]
[-SearchString <String>]
[-MaxResults <Int32>]
[-TenantId <Guid>]
[<CommonParameters>]
In this article, we will discuss how to use the Get-MsolGroup cmdlet in PowerShell to get a list of groups in Azure Active Directory, get a group by name, and get a group by using a user principal name.
Let’s understand the Get-MsolGroup command with examples.
How to Get All Groups in Microsoft 365
To get all groups in Microsoft 365, use the Get-MsolGroup cmdlet.
Get-MsolGroup
This command returns the entire set of groups for the tenant in Microsoft 365. It returns the results up to the default 250 results.
How to Get a Group By Object ID in Office 365
To get a group by name in Office 365, use the Get-MsolGroup cmdlet with -ObjectId
parameter. The -ObjectId
parameter specifies the unique object ID of the group to get.
Get-MsolGroup -ObjectId f876f049-21b1-450d-8961-7e63f49f232e
This command returns the group object that has the specified Object ID.
The output of the above command is given below.
PS C:\> Get-MsolGroup -ObjectId f876f049-21b1-450d-8961-7e63f49f232e
ObjectId DisplayName GroupType Description
-------- ----------- --------- -----------
f876f049-21b1-450d-8961-7e63f49f232e Security DistributionList This is security group.
PS C:\>
How to Get a Group by Using a UserPrincipalName (UPN)
To get a group by using a user principal name in Microsoft 365, use the Get-MsolGroup cmdlet with the -UserPrincipalName
parameter. The -UserPrincipalName
parameter specifies the user’s principal name of a user.
Get-MsolGroup -isAgentRole -UserPrincipalName "[email protected]"
This command returns the security groups to which this user belongs. The -UserPrincipalName
parameter must be used along with the -IsAgentRole
parameter. The -IsAgentRole parameter specifies that the Get-MsolGroup command returns only agent groups.
How to Get Groups that Start with a Specific Name
To get groups that start with a specific name in Microsoft 365, use the Get-MsolGroup cmdlet in PowerShell to get all groups and pipe them to the Where-Object
cmdlet. The Where-Object
command checks the condition if the group display name starts with a specified word.
Get-MsolGroup | Where-Object {$_.DisplayName -like "*Finance*"}
This command returns the group having the “Finance” in their display name in Office 365.
PS C:\> Get-MsolGroup | Where-Object {$_.DisplayName -like "*Finance*"}
ObjectId DisplayName GroupType Description
-------- ----------- --------- -----------
008f363f-5b3a-4899-8004-a2d140426802 Finance DistributionList This is the Finance Group
PS C:\>
How to Search for Group in Microsoft 365
To search for groups in Microsoft 365, use the Get-MsolGroup cmdlet in PowerShell with the -SearchString
parameter. The -SearchString
parameter specifies a string.
Get-MsolGroup -SearchString "Security"
The Get-MsolGroup command returns the security groups that have a display name that starts with the string “Security“.
PS C:\> Get-MsolGroup -SearchString "Security"
ObjectId DisplayName GroupType Description
-------- ----------- --------- -----------
f876f049-21b1-450d-8961-7e63f49f232e Security DistributionList This is security group.
How to Get Security Groups Using Group Type
To get security groups using group type, use the Get-MsolGroup cmdlet with the -GroupType
parameter. The -GroupType
parameter specifies the type of groups to get. Valid values are Security, MailEnabledSecurity, and DistributionList.
Get-MsolGroup -GroupType "Security" | Export-Csv -Path D:\PS\distributionlistGroups.csv -NoTypeInformation
This command returns all the groups by group type “Security” and pipes them to the Export-CSV cmdlet. The Export-CSV command exports the security groups. It uses the -Path
parameter to specify the destination location path.
Conclusion
I hope the above article on how to use the Get-MsolGroup cmdlet in PowerShell to get Microsoft 365 groups is helpful to you.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.