在 Microsoft Entra ID 中批量删除用户

使用属于 Microsoft Entra 的 Microsoft Entra ID 中的管理中心,你可以通过逗号分隔值 (CSV) 文件批量删除用户,从而删除组的大量成员。

批量删除用户

提示

本文中的步骤可能因开始使用的门户而略有不同。

  1. 至少以用户管理员身份登录到 Microsoft Entra 管理中心

  2. 选择“Microsoft Entra ID”。

  3. 选择“用户”>“所有用户”>“批量操作”>“批量删除”。

    “用户”页面的屏幕截图,其中选择了“批量删除”选项。

  4. 在“批量删除用户”页面中,选择“下载”以下载最新版本的 CSV 模板 。

  5. 打开 CSV 文件,并为每个要删除的用户添加一行。 唯一需要的值为“用户主体名称”。 保存文件。

  6. 在“批量删除用户”页的“上传 csv 文件”下,浏览到该文件。 选择该文件并单击“提交”后,将启动对 CSV 文件的验证。

  7. 验证文件内容后,会看到“文件上传成功”消息。 如果有错误,必须修正错误,然后才能提交作业。

  8. 文件通过验证后,请选择“提交”,以启动用于删除用户的批量操作

  9. 删除操作完成后,会显示一条通知,指出批量操作成功。

如果遇到错误,可以在“批量操作结果”页下载并查看结果文件。 该文件包含每个错误的原因。 文件提交必须与提供的模板匹配,并包含确切的列名称。 有关批量操作限制的详细信息,请参阅批量删除服务限制

CSV 模板结构

下面下载的 CSV 模板示例中的行如下所示:

  • 版本号:包含版本号的第一行必须包含在上传的 CSV 中。
  • 列标题User name [userPrincipalName] Required。 较旧版本的模板可能会有所不同。
  • 示例行:我们已在模板中包含可接受值的示例。 Example: chris@contoso.com 你必须删除示例行并将其替换为自己的项。

该屏幕截图显示 CSV 文件包含要删除的用户的名称和 ID。

适用于 CSV 模板的其他指导

  • 不得删除或修改模板的前两行,否则无法处理模板。
  • 所需的列会先列出。
  • 请勿将新列添加到模板。 所添加的任何其他列都会被忽略,不进行处理。
  • 在执行新更改之前,请下载最新版本的 CSV 模板。

查看状态

可在“批量操作结果”页面中查看所有挂起的批量请求的状态。

该屏幕截图显示如何在“批量操作结果”页面中查看删除状态。

接下来,可通过门户或使用 PowerShell 查看已删除的用户是否存在于 Microsoft Entra 组织中。

验证已删除的用户

  1. 至少以用户管理员身份登录到 Microsoft Entra 管理中心
  2. 选择“Microsoft Entra ID”。
  3. 仅选择“所有用户”,并验证是否不再列出已删除的用户。

使用 PowerShell 验证已删除的用户

运行以下命令:

Get-MgUser -Filter "UserType eq 'Member'"

验证已删除的用户是否不再列出。

批量删除服务限制

应注意,每个批量操作活动最多可以运行一小时。

Microsoft Entra 管理门户中的批量操作可能会超时,并可能在非常大的租户上失败。 此限制是一个由缩放限制导致的已知问题。 Microsoft 工程团队正在研究一项最终将解决此限制的新服务。

注意

执行批量操作(如导入或创建)时,如果批量操作未在一小时内完成,则可能是遇到了问题。 若要解决此问题,建议拆分每批处理的记录数。 例如,在开始导出之前,可以通过筛选组类型或用户名来限制结果集,以减少结果的大小。 通过优化筛选器,实质上你是在限制批量操作返回的数据。

此问题的另一种解决方法是使用 PowerShell 直接进行 Microsoft Graph API 调用。 对于批量下载用户和组失败,我们建议使用 PowerShell cmdlet GET-MgGroup -AllGET-MgUser -All

以下 PowerShell 代码示例适用于与以下场景相关的批量操作:

用户

批量下载所有用户

# Import the Microsoft Graph module 
Import-Module Microsoft.Graph 

# Authenticate to Microsoft Graph (you may need to provide your credentials) 
Connect-MgGraph -Scopes "User.Read.All" 

# Get all users using Get-MgUser 
$users = Get-MgUser -All -ConsistencyLevel eventual -Property Id, DisplayName, UserPrincipalName,UserType,OnPremisesSyncEnabled,CompanyName,CreationType 

# Specify the output CSV file path 
$outputCsvPath = "C:\\Users\\YourUsername\\Documents\\Users.csv"  

# Create a custom object to store user data 
$userData = @() 

# Loop through each user and collect relevant data 
foreach ($user in $users) { 
    $userObject = [PSCustomObject]@{ 
        Id = $user.Id 
        DisplayName = $user.DisplayName 
        UserPrincipalName = $user.UserPrincipalName 
        UserType = $user.UserType 
        OnPremisesSyncEnabled = $user.OnPremisesSyncEnabled 
        CompanyName = $user.CompanyName 
        CreationType = $user.CreationType 
    } 
    $userData += $userObject 
} 

# Export user data to a CSV file 
$userData | Export-Csv -Path $outputCsvPath -NoTypeInformation 

# Disconnect from Microsoft Graph 
Disconnect-MgGraph 

Write-Host "User data exported to $outputCsvPath" 

批量创建用户

# Import the Microsoft Graph module 
Import-Module Microsoft.Graph 

# Authenticate to Microsoft Graph (you may need to provide your credentials) 
Connect-MgGraph -Scopes "User.ReadWrite.All" 

# Specify the path to the CSV file containing user data 
$csvFilePath = "C:\\Path\\To\\Your\\Users.csv" 

# Read the CSV file (adjust the column names as needed) 
$usersData = Import-Csv -Path $csvFilePath 

# Loop through each row in the CSV and create users \
foreach ($userRow in $usersData) { 
    $userParams = @{ 
        DisplayName = $userRow.'Name [displayName] Required' 
        UserPrincipalName = $userRow.'User name [userPrincipalName] Required' 
        PasswordProfile = @{ 
            Password = $userRow.'Initial password [passwordProfile] Required' 
        } 
        AccountEnabled = $true 
        MailNickName = $userRow.mailNickName 
    } 
    try { 
        New-MgUser @userParams 
        Write-Host "User $($userRow.UserPrincipalName) created successfully." 
    } catch { 
        Write-Host "Error creating user $($userRow.UserPrincipalName): $($_.Exception.Message)" 
    } 
} 

# Disconnect from Microsoft Graph 
Disconnect-MgGraph 

Write-Host "Bulk user creation completed." 

注意

请确保 CSV 文件包含必要的列(例如 DisplayNameUserPrincipalName 等)。 此外,请调整脚本以匹配 CSV 文件中的实际列名。

批量删除用户

# Import the Microsoft Graph module 
Import-Module Microsoft.Graph 

# Authenticate to Microsoft Graph (you may need to provide your credentials) 
Connect-MgGraph -Scopes "User.ReadWrite.All" 

# Specify the path to the CSV file containing user data 
$csvFilePath = "C:\\Path\\To\\Your\\Users.csv" 

# Read the CSV file (adjust the column names as needed) 
$usersData = Import-Csv -Path $csvFilePath 

# Loop through each row in the CSV and delete users 
foreach ($userRow in $usersData) { 
    try { 
        Remove-MgUser -UserId $userRow.UserPrincipalName -Confirm:$false 
        Write-Host "User $($userRow.UserPrincipalName) deleted successfully." 
    } catch { 
        Write-Host "Error deleting user $($userRow.UserPrincipalName): $($_.Exception.Message)" 
    } 
} 

# Disconnect from Microsoft Graph 
Disconnect-MgGraph 

Write-Host "Bulk user deletion completed." 

注意

请确保 CSV 文件包含必要的列(例如 UserPrincipalName)。 此外,请调整脚本以匹配 CSV 文件中的实际列名。

Groups

批量下载所有组

Import-Module Microsoft.Graph.Groups 

 # Authenticate to Microsoft Graph (you may need to provide your credentials) 
 Connect-MgGraph -Scopes "Group.Read.All" 

 # Get the group members 
 $groups = Get-MgGroup -All | Select displayName, Id, groupTypes,mail 

 # Create a custom object to store group data 
$groupData = @() 

# Loop through each group and collect relevant data 
foreach ($group in $groups) { 
    if ($group.groupTypes -contains "Unified"){$groupType = "Microsoft 365"} 
    else {$groupType = "Security"} 
    if ($group.groupTypes -contains "DynamicMembership"){$membershipType = "Dynamic"} 
    else {$membershipType = "Assigned"} 
    $groupObject = [PSCustomObject]@{ 
        Id = $group.Id 
        DisplayName = $group.displayName 
        Mail = $group.mail 
        GroupType = $groupType 
        MemebershipType = $membershipType 
    }   
    $groupData += $groupObject 
} 

 # Specify the output CSV file path 
 $outputCsvPath = "C:\\Users\\cewu\\Documents\\Groups.csv" 

 $groupData| Export-Csv -Path $outputCsvPath -NoTypeInformation 
 
 Write-Host "Group members exported to $outputCsvPath" 

批量下载组成员

Import-Module Microsoft.Graph.Groups 

 # Authenticate to Microsoft Graph (you may need to provide your credentials) 
 Connect-MgGraph -Scopes "Group.Read.All,GroupMember.Read.All" 

 # Set the group ID of the group whose members you want to download 
 $groupId = "your_group_id" 

 # Get the group members 
 $members = Get-MgGroupMember -GroupId $groupId -All | select * -ExpandProperty additionalProperties | Select-Object @( 
                'id'     
                @{  Name       = 'userPrincipalName' 
                    Expression = { $_.AdditionalProperties["userPrincipalName"] } 
                } 
                @{  Name = 'displayName' 
                Expression = { $_.AdditionalProperties["displayName"] } 
                } 
            ) 

 # Specify the output CSV file path 
 $outputCsvPath = "C:\\Users\\YourUserName\\Documents\\GroupMembers.csv" 

 $members| Export-Csv -Path $outputCsvPath -NoTypeInformation 

# Disconnect from Microsoft Graph 
Disconnect-MgGraph 

 Write-Host "Group members exported to $outputCsvPath"  

批量添加成员

Import-Module Microsoft.Graph.Groups 

 # Authenticate to Microsoft Graph (you may need to provide your credentials) 
 Connect-MgGraph -Scopes "GroupMember.ReadWrite.All" 

# Import the CSV file 
$members = Import-Csv -Path "C:\path\to\your\file.csv" 

# Define the Group ID 
$groupId = "your-group-id" 

# Iterate over each member and add them to the group 
foreach ($member in $members) { 
    try{ 
        New-MgGroupMember -GroupId $groupId -DirectoryObjectId $member.memberObjectId 
  	 Write-Host "Added $($member.memberObjectId) to the group."  
    } 
    Catch{ 
        Write-Host "Error adding member $($member.memberObjectId):$($_.Exception.Message)" 
    } 
} 

# Disconnect from Microsoft Graph 
Disconnect-MgGraph 

批量删除成员

Import-Module Microsoft.Graph.Groups 

 # Authenticate to Microsoft Graph (you may need to provide your credentials) 
 Connect-MgGraph -Scopes "GroupMember.ReadWrite.All" 

# Import the CSV file 
$members = Import-Csv -Path "C:\path\to\your\file.csv" 

# Define the Group ID 
$groupId = "your-group-id" 

# Iterate over each member and add them to the group 
foreach ($member in $members) { 
    try{ 
        Remove-MgGroupMemberByRef -GroupId $groupId -DirectoryObjectId $member.memberObjectId \
        Write-Host "Removed $($member.memberObjectId) from the group." 
    } 
    Catch{ 
        Write-Host "Error removing member $($member.memberObjectId):$($_.Exception.Message)" 
    } 
} 

# Disconnect from Microsoft Graph 
Disconnect-MgGraph 

设备

批量下载所有设备

Import-Module Microsoft.Graph 

 # Authenticate to Microsoft Graph (you may need to provide your credentials) 
 Connect-MgGraph -Scopes "Device.Read.All" 

 # Get all devices  
 $devices = Get-MgDevice -All |select displayName,deviceId,operatingSystem,operatingSystemVersion,isManaged,isCompliant,mdmAppId,registeredOwners,TrustType 

 # Specify the output CSV file path 
 $outputCsvPath = "C:\\Users\\YourUserName\\Documents\\Devices.csv" 

 $devices| Export-Csv -Path $outputCsvPath -NoTypeInformation 

 Write-Host "Devices exported to $outputCsvPath"  

后续步骤