Remove CSV List of users as Member to Azure AD group via Powershell

Joao Manoel Castilho Franco 20 Reputation points
2023-10-20T21:01:06.06+00:00

I'm trying to remove users from a csv file in Azure AD and the command outputs that the account was removed, but it wasn't.

the script is pointing out a failure on line 9 could not find a parameter that matches the parameter name

Any ideas or ways I could adjust?

I can add the same users in bulk to a group, but removing them still generates this error.

PS C:\Windows\system32> $GroupName = "M365_F1"
$UserList = Import-Csv -Path "C:\temp\test_licenses_m365_F1.csv"

foreach ($User in $UserList) {
    $UserPrincipalName = $User.UserPrincipalName
    $Group = Get-AzureADGroup -Filter "DisplayName eq '$GroupName'"
    $UserToRemove = Get-AzureADUser -Filter "UserPrincipalName eq '$UserPrincipalName'"
    if ($UserToRemove -ne $null -and $Group -ne $null) {
        Remove-AzureADGroupMember -ObjectId $Group.ObjectId -Members $UserToRemove.ObjectId
        Write-Host "Removed $UserPrincipalName from $GroupName"
    } else {
        Write-Host "User or Group not found: $UserPrincipalName or $GroupName"
    }
}
Remove-AzureADGroupMember : A parameter cannot be found that matches parameter name 'Members'.
At line:9 char:61
+ ...  Remove-AzureADGroupMember -ObjectId $Group.ObjectId -Members $UserTo ...
+                                                          ~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Remove-AzureADGroupMember], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.Open.AzureAD16.PowerShell.RemoveGroupMember
 
Removed ******@abclab.onmicrosoft.com from M365_F1
Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Microsoft Entra | Other
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2023-10-20T22:24:36.0233333+00:00

    Hi @Joao Manoel Castilho Franco ,

    please try this:

    $GroupName = "M365_F1"
    $UserList = Import-Csv -Path "C:\temp\test_licenses_m365_F1.csv"
    foreach ($User in $UserList) {
        $UserPrincipalName = $User.UserPrincipalName
        $Group = Get-AzureADGroup -Filter "DisplayName eq '$GroupName'"
        $UserToRemove = Get-AzureADUser -Filter "UserPrincipalName eq '$UserPrincipalName'"
        if ($UserToRemove -ne $null -and $Group -ne $null) {
            Remove-AzureADGroupMember -ObjectId $Group.ObjectId -MemberId $UserToRemove.ObjectId
            Write-Host "Removed $UserPrincipalName from $GroupName"
        }
        else {
            Write-Host "User or Group not found: $UserPrincipalName or $GroupName"
        }
    }
    

    The parameter of Remove-AzureADGroupMember should be -MemberId instead of -Members.


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards Andreas Baumgarten

    2 people found this answer helpful.

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.