Mostefai Mohammed Amine's Blog


Hi and welcome to my blog. I share in this space a lot of posts related to software architecture, and software development. Content is mainly related to .NET CORE development, Angular, Sharepoint, Azure and Office 365. I hope that my articles are helpful and that you enjoy using them 😉

Useful scripts for Group manipulation in Office 365

Hi, let me share with you two useful powershell scripts that permit to list the office 365 groups with the member count and to move users from a group to another group. Of course, you are supposed to be connected to your Office 365 using Connect-MsolService commandlet. Listing the groups with member count Of course, there is the Get-MsolGroup that list all the office 365 groups but we are obliged to call Get-MsolGroupMember individually to get the members and their count of a given group. The script below lists the existing group and counts their members. We use the measure pipeline to avoid data loading overhead. Write-Host "=======================" -ForegroundColor GreenWrite-Host "Listing groups and member count" -ForegroundColor GreenWrite-Host "=======================" -ForegroundColor Green$groups = Get-MsolGroup Write-Host "Got $($groups.Count) groups" -ForegroundColor Yellow$i = 1$groups | ForEach-Object { $members = Get-MsolGroupMember -GroupObjectId $_.ObjectId | measure Write-Host "group [$($i) of $($groups.Count)] : $($_.DisplayName) | members: $($members.Count)" -ForegroundColor Cyan $i = $i + 1} Moving Users from Groups The second script is very useful to group management. The purpose of this script is to move all the users of a source group to a destination group. It takes two parameters, the object ids of the source and destination groups. Param([Guid] $source, [Guid] $destination)Write-Host "=======================" -ForegroundColor GreenWrite-Host "Moving users from group $($source) to destination $($destination)" -ForegroundColor GreenWrite-Host "=======================" -ForegroundColor Green$sourceGroup = Get-MsolGroup -ObjectId $sourceWrite-Host "source group name $($sourceGroup.DisplayName)" -ForegroundColor Yellow$destGroup = Get-MsolGroup -ObjectId $destinationWrite-Host "destination group name : $($destGroup.DisplayName)" -ForegroundColor YellowWrite-Host "loading members from source group" -ForegroundColor Yellow$members = Get-MsolGroupMember -GroupObjectId $sourceWrite-Host "loaded source group members, count : $($members.Count)" -ForegroundColor Yellow$i = 1$members | ForEach-Object { Write-Host "moving user $($_.DisplayName), [$($i) of $($members.Count)]" -ForegroundColor Cyan Add-MsolGroupMember -GroupObjectId $destination -GroupMemberObjectId $_.ObjectId Remove-MsolGroupMember -GroupObjectId $source -GroupMemberObjectId $_.ObjectId Write-Host "user successfully moved" -ForegroundColor Cyan $i = $i + 1}If($i -gt 1){ Write-Host "successfully moved $($members.Count) users" -ForegroundColor Green $members = Get-MsolGroupMember -GroupObjectId $source Write-Host "source group members count : $($members.Count)" -ForegroundColor Green $members = Get-MsolGroupMember -GroupObjectId $destination Write-Host "destination group members count : $($members.Count)" -ForegroundColor Green} Enjoy !