I don't know how to delete members of a group using the Graph API in PowerShell.

松田 大知 36 Reputation points
2022-05-16T02:10:25.327+00:00

This thread is a continuation of the thread linked below.

https://learn.microsoft.com/en-us/answers/questions/847858/powershell%20%20%20%20graph-api%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20.html?childToView=850068#comment-850068

Original question:

Blockquote

I see from your reference that there is no PowerShell cmdlet to remove a member from a group in GraphAPI.
https://learn.microsoft.com/ja-jp/graph/api/group-delete-members?view=graph-rest-1.0&tabs=http
In other words, am I correct in assuming that it is not possible to delete members from a group using GraphAPI from PowerShell?

Blockquote

@CarlZhao-MSFT
Thank you for all your help.

I tried the script you gave me and it deleted the user object itself.

I would like to delete the user object of a particular member from a group and have the same user object join another different group.

What script would be needed in this case?
I would appreciate it if you could tell me.

Microsoft Security | Microsoft Graph
{count} votes

Accepted answer
  1. CarlZhao-MSFT 46,376 Reputation points
    2022-05-17T02:25:35.893+00:00

    Hi @松田 大知

    As we discussed in this thread, the api documentation doesn't seem to provide a PowerShell command to remove group members, there is a similar issue on GitHub.

    As an alternative solution, you can refer to this script to remove group members:

     $clientID = " "  
     $secretKey = " "  
     $tenantID = " "  
     $username = " "  
     $password = " "  
              
      $authUrl = "https://login.microsoftonline.com/" + $tenantID + "/oauth2/v2.0/token/"  
      $body = @{  
         "scope" = "https://graph.microsoft.com/.default";  
         "grant_type" = "password";  
         "client_id" = $ClientID;  
         "client_secret" = $secretKey;  
         "username" = $username;  
         "password" = $password  
          
      }  
      $authToken = Invoke-RestMethod -Uri $authUrl –Method POST -Body $body  
             
      $url = "https://graph.microsoft.com/v1.0/groups/{group-id}/members/{directory-object-id}/`$ref"  
      $headers = @{  
      "Authorization" = "Bearer $($authToken.access_token)"  
      }  
          
     Invoke-RestMethod -Uri $url -Headers $headers -Method DELETE  
    

    Of course, you can also add user objects to a different group:

    Import-Module Microsoft.Graph.Groups  
      
    $params = @{  
    	"@odata.id" = "https://graph.microsoft.com/v1.0/directoryObjects/{id}"  
    }  
      
    New-MgGroupMemberByRef -GroupId $groupId -BodyParameter $params  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.