Managing Microsoft 365 Groups is a vital task for administrators who want to streamline collaboration across their organization. This can involve tasks like creating new groups, updating group details, listing all groups, or even deleting unnecessary ones. Microsoft Graph PowerShell offers a powerful way to automate these tasks efficiently.
In this article, we will explore a Graph PowerShell based script designed to manage Microsoft 365 Groups. It helps Microsoft 365 administrator perform the following actions: 1) List all groups, 2) Create a new group, 3) Update an existing group’s description and 4) Delete a group. The 5th option exits the script.
# Ensure the Microsoft Graph PowerShell module is installed and imported
if (-not (Get-Module -ListAvailable -Name "Microsoft.Graph")) {
Install-Module -Name "Microsoft.Graph" -Scope CurrentUser
}
Import-Module Microsoft.Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All"
# Function to list all M365 Groups
function Get-AllGroups {
try {
Get-MgGroup -All | ForEach-Object {
[PSCustomObject]@{
DisplayName = $_.DisplayName
MailNickname = $_.MailNickname
Description = $_.Description
GroupId = $_.Id
GroupType = $_.GroupTypes
}
} | Format-Table -AutoSize
} catch {
Write-Host "Error retrieving groups: $($_.Exception.Message)" -ForegroundColor Red
}
}
# Function to create a new M365 Group
function New-M365Group {
$groupDisplayName = Read-Host "Enter the group display name"
$groupMailNickname = Read-Host "Enter the group mail nickname"
$groupDescription = Read-Host "Enter the group description"
$groupVisibility = Read-Host "Enter group visibility (Private/Public)"
try {
$groupParams = @{
displayName = $groupDisplayName
mailEnabled = $true
mailNickname = $groupMailNickname
securityEnabled = $false
description = $groupDescription
groupTypes = @("Unified")
visibility = $groupVisibility
}
$newGroup = New-MgGroup -BodyParameter $groupParams
Write-Host "New Group Created: $($newGroup.Id)"
} catch {
Write-Host "Error creating group: $($_.Exception.Message)" -ForegroundColor Red
}
}
# Function to update M365 Group details
function Update-M365Group {
$groupDisplayName = Read-Host "Enter the current group display name"
$newGroupDescription = Read-Host "Enter the new description for the group"
try {
$groupId = (Get-MgGroup -Filter "displayName eq '$groupDisplayName'").Id
if ($groupId) {
$groupParams = @{
description = $newGroupDescription
}
Update-MgGroup -GroupId $groupId -BodyParameter $groupParams
Write-Host "Group updated successfully!"
} else {
Write-Host "Group not found!" -ForegroundColor Red
}
} catch {
Write-Host "Error updating group: $($_.Exception.Message)" -ForegroundColor Red
}
}
# Function to delete an M365 Group
function Remove-M365Group {
$groupDisplayName = Read-Host "Enter the group display name to delete"
try {
$groupId = (Get-MgGroup -Filter "displayName eq '$groupDisplayName'").Id
if ($groupId) {
Remove-MgGroup -GroupId $groupId
Write-Host "Group removed successfully!"
} else {
Write-Host "Group not found!" -ForegroundColor Red
}
} catch {
Write-Host "Error deleting group: $($_.Exception.Message)" -ForegroundColor Red
}
}
# Main Menu
function Show-Menu {
Write-Host "What action would you like to perform?"
Write-Host "1. List all groups"
Write-Host "2. Create a new group"
Write-Host "3. Update an existing group's description"
Write-Host "4. Delete a group"
Write-Host "5. Exit"
$action = Read-Host "Enter your choice (1-5)"
return $action
}
# Main logic
do {
$action = Show-Menu
switch ($action) {
1 { Get-AllGroups }
2 { New-M365Group }
3 { Update-M365Group }
4 { Remove-M365Group }
5 { Write-Host "Exiting script..."; break }
default { Write-Host "Invalid selection. Please choose a valid option." }
}
$continue = Read-Host "Do you want to perform another action? (yes/no)"
} while ($continue -eq 'yes')
See the Script in Action by clicking and playing this GIF:
This script provides an interactive way for administrators to manage Microsoft 365 Groups. Here's how each function works:
There are several ways to further enhance this script for more robust Microsoft 365 Group management:
With this interactive Microsoft 365 Group management script, administrators can streamline group creation, updating, listing, and deletion processes using Microsoft Graph PowerShell. The script is flexible, allowing you to add additional features, such as bulk operations and error logging, to tailor it to your organization's needs. By handling key management tasks directly from the command line, administrators can save time and reduce the chances of manual errors. Keep this script handy for your day-to-day group management, and enhance it as your needs evolve!
© m365corner.com. All Rights Reserved. Design by HTML Codex