Delete Microsoft 365 Group Based on Created Date
This Graph Powershell script helps you bulk delete Microsoft 365 groups based on the date they were created (created date). It requires you to pass a date which it accepts. Then makes use of Get-MgGroup to fetch all the Microsoft 365 groups created on that day and deletes each one of them by executing Remove-Mggroup cmdlet. Since deletion is a sensitive operation, the script requires you to confirm the deletion before it actually happens.
Prerequisites
- You need to install the Microsoft Graph PowerShell SDK. Install-Module Microsoft.Graph -Scope CurrentUser is the command.
- You need to connect to the Microsoft Graph PowerShell Module with the necessary permissions. Connect-MgGraph -Scopes "Group.ReadWrite.All" is the command.
Delete Microsoft 365 Group Based on Created Date PowerShell Script
The script installs and connects PowerShell to Microsoft Graph PowerShell module. Then fetches all the Microsoft 365 groups(using Get-MgGroup cmdlet) created in your organization based on the date you specify in the script. Finally, deletes the fetched groups using Remove-MgGroup cmdlet.
When you execute the script, you should get a response that lists all the Microsoft 365 groups created on the specified date. Then you'll be asked whether you wish to continue with the deletion. When you confirm the deletion, the list of deleted groups get displayed.:
How the Script Works?
The Delete Microsoft 365 Group Based on Created Date script does the following:
Step 1: Install and Import the Necessary Module
- Install the Microsoft Graph PowerShell Module: This step ensures that the necessary module (Microsoft.Graph) is installed in your PowerShell environment, allowing you to use cmdlets that interact with Microsoft Graph API.
- Connect to Microsoft Graph: You connect to Microsoft Graph using the Connect-MgGraph cmdlet. This requires you to have the appropriate permissions, in this case, Group.ReadWrite.All to read and delete groups.
Step 2: Define Parameters and Fetch Data
- Define the Date: You specify the date for which you want to delete groups. This is crucial as it determines which groups are targeted for deletion.
- Fetch Groups Created on Specified Date: The script uses Get-MgGroup to retrieve all groups, then filters these groups with Where-Object to find those created on the specified date.
Step 3: User Confirmation for Deletion
- Display Groups to Be Deleted: Before proceeding with deletion, the script lists all the groups that match the criteria. This is a safety step to ensure that you are aware of which groups are about to be deleted..
- Confirmation Prompt: The script prompts you to confirm the deletion. This is an essential step to prevent accidental deletions. If the confirmation is "Y" (yes), the script proceeds; if not, it cancels the operation.
Step 4: Perform Deletion
- Delete Groups: If confirmed, the script iterates over the filtered groups and deletes each one using the Remove-MgGroup cmdlet. It logs the name of each group as it is deleted, providing a clear record of the action taken.
Step 5: Clean Up and Disconnect
- Disconnect from Microsoft Graph: Finally, the script disconnects from Microsoft Graph using Disconnect-MgGraph. This is a good practice to end the session securely and free up resources..
Further Enhancing the Script
Enhancing the script for deleting Microsoft 365 groups based on a specific creation date can be approached from several angles, focusing on adding more functionality, improving user experience, and ensuring better safety and compliance. Here are some suggestions:
Logging and Audit Trails
- Enhanced Logging: Integrate comprehensive logging that records not only the deletion but also who performed the action and when. This can be crucial for audits and troubleshooting.
- Export Log to File: Save the deletion logs to a secure location or a file for audit purposes.
Advanced Filtering Options
- Multiple Date Criteria: Allow the script to handle multiple dates or a range of dates. This can be useful for batch processing.
- Include Additional Filters: For example, filter groups based on their types, membership size, or other attributes that might be relevant to your organizational policies..
Safety Features
- Dry Run Mode: Implement a 'dry run' option that lists what would be deleted without actually performing the deletions. This can help verify the correctness before executing..
- Backup Options: Offer a way to archive or backup group details before deletion, ensuring data can be restored if deleted inadvertently.
Performance Improvements
- Batch Processing: Modify the script to handle large volumes of data more efficiently, perhaps by processing in batches or using asynchronous operations where appropriate..
- Error Handling: Improve error handling to manage and retry failed deletions, and provide clear messages about what went wrong.
User Interaction and Accessibility
- Interactive Menu: Develop an interactive menu for users to select from multiple options, enhancing the script’s usability.
- Parameterization: Allow parameters to be passed via command line or through a configuration file, making the script more flexible and easier to integrate with other tools.
Security Enhancements
- Role-Based Access: Check the user's role before allowing them to execute deletions, ensuring that only authorized personnel can delete groups.
- Encryption: When exporting any data, ensure it is encrypted, especially if it includes sensitive or personally identifiable information.
Integration with IT Management Tools
- Automation and Scheduling: Integrate the script with task schedulers (like Windows Task Scheduler) to run deletions at predefined times, reducing manual intervention.
- Notifications: Send notifications (emails or alerts) to administrators or audit teams when groups are deleted, especially for critical or high-impact operations.
Related Articles:
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
Import M365 Users to Microsoft Teams from CSV file
Get Microsoft 365 Group Owners List Using Graph PowerShell
Get Microsoft 365 Group Members Using Graph PowerShell