Migrating from Add-MsolGroupMember to New-MgGroupMember

As Microsoft phases out the MSOnline module, PowerShell admins must shift to using the Microsoft Graph PowerShell SDK. One of the most frequent tasks for any Microsoft 365 administrator is adding users to groups — and for years, Add-MsolGroupMember was the go-to cmdlet.

Now, it’s time to switch to New-MgGroupMember. In this guide, we’ll explore how to make the transition, compare the differences, and provide practical examples for your updated scripts.


What You Did Previously with Add-MsolGroupMember

The Add-MsolGroupMember cmdlet was simple and widely used to assign users to Microsoft 365 groups by referencing their email or object ID.

Add a Single User to a Group

Add-MsolGroupMember -GroupObjectId "d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c" `
-GroupMemberObjectId "5c5d5f65-1d6b-4141-a5e5-b8c85d0c6e8f" `
-GroupMemberType User
                                        

Add Users from a CSV File

$members = Import-Csv -Path "C:\\path\\to\\members.csv"
foreach ($member in $members) {
    Add-MsolGroupMember -GroupObjectId $member.GroupId `
    -GroupMemberObjectId $member.UserId `
    -GroupMemberType User
}
                                         

While this was functional, it’s no longer future-proof or supported in modern environments.


What You Should Do Now with New-MgGroupMember

The new New-MgGroupMember cmdlet brings Graph API capabilities and more consistent object handling. Here's how the same scenarios work now:

Add a User to a Group

$groupId = "d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c"
$userId = "5c5d5f65-1d6b-4141-a5e5-b8c85d0c6e8f"
                                            
try {
    New-MgGroupMember -GroupId $groupId -DirectoryObjectId $userId
    Write-Host "User with ID $userId has been successfully added to the group with ID $groupId." -ForegroundColor Green
} catch {
    Write-Host "Failed to add user to the group. Error: $_" -ForegroundColor Red
}
                                        

Add Multiple Users to a Group

$groupId = "d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c"
$userIds = @(
    "5c5d5f65-1d6b-4141-a5e5-b8c85d0c6e8f",
    "6d7e8f70-6e7b-41d2-a6f7-9c85d7f16e9d"
)
                                            
foreach ($userId in $userIds) {
    try {
        New-MgGroupMember -GroupId $groupId -DirectoryObjectId $userId
        Write-Host "User with ID $userId successfully added to the group with ID $groupId." -ForegroundColor Green
    } catch {
        Write-Host "Failed to add user with ID $userId to the group. Error: $_" -ForegroundColor Red
    }
}
                                        

Add Members from a CSV File

$csvPath = "C:\\path\\to\\your\\members.csv"
$members = Import-Csv -Path $csvPath
                                            
foreach ($member in $members) {
    $user = Get-MgUser -UserPrincipalName $member.UserPrincipalName
    New-MgGroupMember -GroupId $member.GroupId -DirectoryObjectId $user.Id
}
                                        

CSV File Format

GroupId,UserPrincipalName
d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c,john.doe@yourdomain.com
d9f6b5c5-67e5-41d1-9af0-8c85b6f15d0c,jane.smith@yourdomain.com
                                        

What’s Different with New-MgGroupMember?


Old (Add-MsolGroupMember) New (New-MgGroupMember)
Accepts GroupObjectId and UserObjectId Accepts GroupId and DirectoryObjectId
Requires GroupMemberType Automatically assumes User or Object
Part of deprecated MSOnline module Part of Microsoft.Graph.Groups module
Limited to users Supports users, service principals, and devices
Simple error reporting Enables try/catch blocks for better control

Conclusion

The shift from Add-MsolGroupMember to New-MgGroupMember is a necessary step for admins preparing their automation scripts for the future. While the syntax is a bit more modern, the flexibility and power of Microsoft Graph make it well worth the effort.

If you're still using MSOnline, now is the perfect time to transition.

Stay ahead of the curve and keep your admin toolbox updated with Microsoft Graph!

Head over to M365Corner.com and explore more real-world migration examples and tools designed for Microsoft 365 admins.



Permission Required

Example:


                                


                                


                                

© Your Site Name. All Rights Reserved. Design by HTML Codex