Listing Microsoft 365 Groups Created Within Last Month

Managing Microsoft 365 Groups efficiently is crucial for IT administrators. Knowing which groups have been created recently helps in keeping track of changes within your organization. This article provides a PowerShell script using Microsoft Graph to list all Microsoft 365 groups created within the last month and includes the total count of these groups.


PowerShell Script

Below is the script that connects to Microsoft Graph retrieves all groups filters them to find those created within the last month and displays the relevant details along with the count.

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.Read.All"

# Get the current date and the date one month ago
$currentDate = Get-Date
$oneMonthAgo = $currentDate.AddMonths(-1)

# Fetch all groups
$allGroups = Get-MgGroup -All

# Filter groups created within the last month
$recentGroups = $allGroups | Where-Object { $_.CreatedDateTime -ge $oneMonthAgo }

# Display the groups
$recentGroups | Select-Object DisplayName Description Mail CreatedDateTime

# Display the total count of groups created within the last month
$groupCount = $recentGroups.Count
Write-Output "Total groups created within the last month: $groupCount"

# Disconnect from Microsoft Graph
Disconnect-MgGraph

Script Output


Script Explanation

Connect to Microsoft Graph

Connect-MgGraph -Scopes "Group.Read.All"

This command connects to Microsoft Graph with the necessary permissions to read group information.

Get Current Date and Date One Month Ago

$currentDate = Get-Date
$oneMonthAgo = $currentDate.AddMonths(-1)

These commands store the current date and the date one month prior in variables.

Fetch All Groups

$allGroups = Get-MgGroup -All

This command retrieves all groups in the organization.

Filter Groups Created Within the Last Month

$recentGroups = $allGroups | Where-Object { $_.CreatedDateTime -ge $oneMonthAgo }

This command filters the groups to include only those created within the last month.

Display the Groups

$recentGroups | Select-Object DisplayName Description Mail CreatedDateTime

This command selects and displays the desired properties of the filtered groups.

Display the Total Count

$groupCount = $recentGroups.Count
Write-Output "Total groups created within the last month: $groupCount"

These commands count the filtered groups and display the total.

Disconnect from Microsoft Graph

Disconnect-MgGraph

This command disconnects from Microsoft Graph.


Further Enhancements

Automate Reporting

# Schedule this script to run monthly and send a report via email

Add More Filters

# Filter groups based on additional properties such as MailEnabled or SecurityEnabled

Export to CSV

$recentGroups | Export-Csv -Path "RecentGroups.csv" -NoTypeInformation

Export the filtered group information to a CSV file for further analysis or record-keeping.


Use Cases

  • Compliance and Auditing: Ensure that newly created groups comply with organizational policies.
  • Monitoring Changes: Track recent changes in group structures and take necessary actions.
  • Resource Management: Manage and allocate resources based on the creation of new groups.

Possible Errors & Solutions

Insufficient Permissions

Error: ErrorCode: Authorization_RequestDenied

Solution: Ensure that the account running the script has the Group.Read.All permission.

Rate Limiting

Error: ErrorCode: throttledRequest

Solution: Implement retry logic in the script to handle throttling.

Network Issues

Error: Connect-MgGraph : The network path was not found

Solution: Check your network connection and ensure you can reach the Microsoft Graph endpoint.


Conclusion

By using the provided PowerShell script administrators can efficiently list and count Microsoft 365 groups created within the last month. This helps in maintaining an organized and compliant environment. Enhancements like automation additional filters and exporting data can further improve the utility of this script. Keeping an eye on possible errors and their solutions ensures smooth execution and reliable results.

Implement this script in your regular administrative tasks to streamline group management and stay updated with recent changes within your Microsoft 365 environment.


Related Articles:

Using Get-MgDirectoryRole in Graph PowerShell
Using Get-MgUserLicenseDetail in Graph PowerShell
Using Find-GraphMgCommand in Graph PowerShell
Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell
Microsoft 365 Group User Import Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex