Thursday, December 5, 2024
HomePowershellUtilizing the Get-MgGroup cmdlet in PowerShell — LazyAdmin

Utilizing the Get-MgGroup cmdlet in PowerShell — LazyAdmin


The Get-MgGroup cmdlet is a straightforward option to get and handle your Microsoft 365 teams. It lets you get all or particular teams with their properties. The Get-MgGroup cmdlet is a part of the Microsoft Graph SDK for PowerShell, which lets you handle all Microsoft Providers by way of a single endpoint.

ez-toc]

On this article, we’ll take a look at how you should use the Get-MgGroup cmdlet to seek out and retrieve group data from Microsoft Entra.

Necessities

Earlier than you should use the Get-MgGroup cmdlet, it’s good to just remember to have put in the Microsoft Graph PowerShell module. You’ll be able to learn the complete information right here, or use the command under to rapidly set up it:

Set up-Module Microsoft.Graph -Pressure

Additionally, you will want to connect with Microsoft Graph with the proper scopes. To get group data, we solely want the Group.Learn.All scope. Learn extra on connecting to Microsoft Graph and utilizing the scopes in this text.

Join-MgGraph -Scopes 'Group.Learn.All'

Getting Teams

The Get-MgGroup cmdlet lets you discover and extract group data from Microsoft Entra. There are a few parameters that we are able to use to seek out or filter the customers:

  • GroupId  – Return particular group based mostly on UPN or ObjectID
  • Filter – Retrieve a number of objects based mostly on an oDate v3 question
  • Search – Get all teams that match the searchString
  • All  – Return all outcomes (by default the primary 100 gadgets are returned)
  • High – Return n variety of outcomes

Good to know is that the cmdlet solely returns the primary 100 outcomes by default. So just remember to use the -All parameter to get all the outcomes when wanted.

Get Group by ID

On the subject of retrieving objects with Microsoft Graph, we frequently want to make use of the ID of the item to get it. With customers, we are able to use the UserPrincipalName as an alternative of the ID, however with Teams we don’t have that luxurious. The GroupID parameter solely accepts the precise ID.

So to get a bunch based mostly on the ID, you are able to do the next:

Get-MgGroup -GroupId "ded414a7-89fa-4cd1-aa91-34c7d4757a4f"

Get MgGroup by Identify

A extra widespread strategy is to get the group by title. However the Get-MgGroup cmdlet doesn’t have a parameter for the group title. Which means that we should use the -Filter parameter to seek out the group, based mostly on the complete title or part of it.

For the filter, we are able to use the next operators to get the teams:

Operator Description Instance
eq Equals to DisplayName eq ‘M365_E5’
and And
or Or DisplayName eq ‘M365_E3’or DisplayName eq ‘M365_E5’
startswith String begins with startswith(DisplayName,’M365_E3′)

To get a bunch with the title “M365_E5” you need to use the eq operator and filter on the show title:

Get-MgGroup -Filter "DisplayName eq 'M365_E5'"

For those who don’t know the precise title, then the startswith operator can be a superb possibility. It will get all of the teams the place the title begins with the given string:

Get-MgGroup -Filter "startswith(DisplayName,'M365')"
get-mggroup cmdlet

Get Dynamic Teams

One of many harder filters to get a grasp on, is for instance to filter on the GroupTypes. Let’s say you wish to get all dynamics teams from Microsoft Entra. A traditional filter gained’t work, as a result of the property GroupTypes is an object.

Nonetheless, we are able to nonetheless filter on it, utilizing an OData filter like this:

Get-MgGroup -Filter "GroupTypes/any(x:x eq 'DynamicMembership')"

Utilizing Superior Filters

As you’ve gotten seen above, the filter parameter is fairly restricted by default. It’s nonetheless doable to make use of extra superior filters within the Microsoft Graph module. These superior queries are solely obtainable for Microsoft Entra ID objects (listing objects).

To make use of the superior filter, we might want to add two parameters when utilizing the -Filter. We have to set the ConsistencyLevel to Eventual and add a Rely variable. Once you add these two parameters, then you should use the search operators under as nicely.

Operator Description Instance
ne Not equal to DisplayName ne ‘M365_E5’
not and Not
endsWith String ends with endsWith(DisplayName,’E3′)

You could find extra details about the superior question capabilities right here within the Microsoft documentation.

Getting All Teams Created Final Yr

With the superior queries enabled, we are able to for instance get all of the teams which can be created final 12 months. To do that, we’ll filter the teams on the Created Date Time and calculate the date by subtracting one 12 months from the present date.

Observe that now we have added the parameters –ConsistencyLevel eventual and -CountVariable Rely to the cmdlet as nicely:

Get-MgGroup -Filter "CreatedDateTime ge $((Get-Date).AddYears(-1).ToString("s"))Z" -ConsistencyLevel eventual -CountVariable Rely

We are able to now additionally mix this with different operators, to get for instance all of the teams which can be created final 12 months and the place the title begins with “M365” :

Get-MgGroup -Filter "CreatedDateTime ge $((Get-Date).AddYears(-1).ToString("s"))Z and startsWith(Displayname,'M365')" -ConsistencyLevel eventual -CountVariable Rely

Utilizing Search to Discover Teams

Apart from the filter parameter, we are able to additionally use the -Search parameter to seek out teams. The parameter requires a property that you simply wish to search on and a price. Additionally, you will must set the -consistencylevel to eventual.

The benefit of the -search parameter is that it permits us to look on any a part of the worth. So for instance, if wish to search on part of the title we are able to use :

Get-MgGroup -Search 'DisplayName:365' -ConsistencyLevel eventual
get-mggroup by name

A bonus of search is that you should use it on just about all of the properties which can be returned by the cmdlet. So we are able to even search on the outline for instance:

Get-MgGroup -Search 'Description:365' -ConsistencyLevel eventual

Wrapping Up

Getting Microsoft 365 group data is fairly easy with the Get-MgGroup cmdlet. Just be sure you additionally check out the brand new Microsoft Entra PowerShell module, which makes working with Microsoft Graph a bit simpler.

Hope you favored this text, when you have any questions, simply drop a remark under.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments