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.
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
Connect-MgGraph -Scopes "Group.Read.All"
This command connects to Microsoft Graph with the necessary permissions to read group information.
$currentDate = Get-Date
$oneMonthAgo = $currentDate.AddMonths(-1)
These commands store the current date and the date one month prior in variables.
$allGroups = Get-MgGroup -All
This command retrieves all groups in the organization.
$recentGroups = $allGroups | Where-Object { $_.CreatedDateTime -ge $oneMonthAgo }
This command filters the groups to include only those created within the last month.
$recentGroups | Select-Object DisplayName Description Mail CreatedDateTime
This command selects and displays the desired properties of the filtered groups.
$groupCount = $recentGroups.Count
Write-Output "Total groups created within the last month: $groupCount"
These commands count the filtered groups and display the total.
Disconnect-MgGraph
This command disconnects from Microsoft Graph.
# Schedule this script to run monthly and send a report via email
# Filter groups based on additional properties such as MailEnabled or SecurityEnabled
$recentGroups | Export-Csv -Path "RecentGroups.csv" -NoTypeInformation
Export the filtered group information to a CSV file for further analysis or record-keeping.
Error: ErrorCode: Authorization_RequestDenied
Solution: Ensure that the account running the script has the Group.Read.All permission.
Error: ErrorCode: throttledRequest
Solution: Implement retry logic in the script to handle throttling.
Error: Connect-MgGraph : The network path was not found
Solution: Check your network connection and ensure you can reach the Microsoft Graph endpoint.
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.
© m365corner.com. All Rights Reserved. Design by HTML Codex