当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 从 Microsoft 365 中永久删除用户

作者:精品下载站 日期:2024-12-14 03:27:38 浏览:13 分类:玩电脑

从 Microsoft 365 中永久删除用户


如何从 Office 365/Microsoft 365 中永久删除用户?当您从 Microsoft 365 中删除用户时,他们将移至回收站。这称为软删除。软删除用户帐户将保留 30 天。之后,它将被微软自动删除。这称为硬删除。如果您不想等待并想手动删除已删除的用户怎么办?让我们看看如何使用 PowerShell 强制删除 Microsoft 365 用户。

软删除用户与硬删除用户

  • 软删除用户是指已被删除但仍在 Microsoft Entra ID 回收站中的时间少于 30 天的用户。
  • 硬删除用户是已删除且在 Microsoft Entra ID 回收站中不可用的用户。

注意:当您从回收站删除对象(硬删除)时,您无法恢复该对象。使用该命令时要格外小心,因为它是永久性的。

在 Microsoft 365 管理中心查找已删除的用户

您可以在 Microsoft 365 管理中心找到已删除的用户。导航至用户 > 已删除的用户

[玩转系统] 从 Microsoft 365 中永久删除用户

您无法从 Microsoft 365 管理中心删除已删除的用户。删除已删除的 Microsoft 365 用户的一个绝佳方法是使用 PowerShell 和 Microsoft Entra 管理中心。

让我们在下一步中看看。

使用 Microsoft Graph PowerShell 连接到 Microsoft Entra ID

以管理员身份启动 Windows PowerShell 并安装 Microsoft Graph PowerShell。

Install-Module Microsoft.Graph -Force
Install-Module Microsoft.Graph.Beta -AllowClobber -Force

重要提示:始终安装 Microsoft Graph PowerShellMicrosoft Graph Beta PowerShell 模块。这是因为某些 cmdlet 在最终版本中尚不可用,并且无法运行。在运行 cmdlet 或脚本之前将两个模块更新到最新版本,以防止出现错误和不正确的结果。

运行 Connect-MgGraph cmdlet 以启动与 Microsoft Entra ID 的连接。

Connect-MgGraph -Scopes "User.ReadWrite.All"

运行上述 cmdlet 后,将显示登录窗口。填写凭据并登录。

[玩转系统] 从 Microsoft 365 中永久删除用户

使用 PowerShell 连接到 Microsoft Entra ID 后,让我们获取已删除的 Microsoft 365 用户。

获取所有 Microsoft 365 已删除用户

使用 PowerShell 查找 Microsoft 365 已删除的用户。运行以下使用 Get-MgDirectoryDeletedItemAsUser cmdlet 的脚本。

# Connect with privileges
Connect-MgGraph -Scopes "User.ReadWrite.All"

# Get properties
$Properties = @(
    'Id',
    'userPrincipalName',
    'displayName',
    'deletedDateTime',
    'userType'
)

# Retrieve deleted directory items
$DeletedItems = Get-MgDirectoryDeletedItemAsUser -All -Property $Properties | Select-Object $Properties

# Check if there are no deleted accounts
if ($DeletedItems.Count -eq 0) {
    Write-Host "No deleted accounts found in the recycle bin." -ForegroundColor Cyan
}
else {
    # Create an array to store the report
    $Report = @()

    # Loop through the deleted items
    foreach ($Item in $DeletedItems) {
        $DeletedDate = Get-Date($Item.DeletedDateTime)
        $DaysSinceDeletion = (New-TimeSpan $DeletedDate).Days

        # Create a custom object for each item and add it to the report
        $ReportLine = [PSCustomObject]@{
            Id                    = $Item.Id
            UserPrincipalName     = $Item.UserPrincipalName
            'Display Name'        = $Item.DisplayName
            Deleted               = $DeletedDate
            'Days Since Deletion' = $DaysSinceDeletion
            Type                  = $Item.UserType
        }
        $Report += $ReportLine
    }

    # Sort the report by 'Display Name'
    $Report | Sort-Object 'Display Name' | Format-Table
}

输出将显示已删除的 Microsoft 365 用户。

Id                                   UserPrincipalName                                         Display Name    Deleted                Days Since Deletion
--                                   -----------------                                         ------------    -------                -------------------
e09cb6da-5d88-469f-bfbf-ec78b5b4d1e8 [email protected]    Audrey Page     10/29/2023 10:51:54 AM                   0
4b9d97bc-2af6-4424-be3e-79b237193c5e [email protected]     Emily Blake     10/29/2023 10:51:56 AM                   0
ec9e69d8-8db5-47ab-827c-915f1d3fd25e [email protected]    Frank Davies    10/29/2023 10:51:58 AM                   0
205bab92-1422-4cd7-b5f4-70c48af828c2 [email protected]     Jane Graham     10/29/2023 10:52:00 AM                   0
faa16dd9-7b0d-4088-b3f9-ccb5ec34bb44 [email protected]     Joseph Bond     10/29/2023 10:52:01 AM                   0
482d12a1-a803-44a3-8b20-c4b43b65ed12 [email protected]  Leonard Hudson  10/29/2023 10:52:03 AM                   0
41981ec4-5106-4b0e-b00e-9605b6d820cb [email protected]  Peter Morrison  10/29/2023 10:52:05 AM                   0
e6160ee2-0d56-4763-899c-950bd53e0ac6 [email protected] Rachel Hamilton 10/29/2023 10:52:07 AM                   0
86945f71-b2db-46b3-9e38-4652e09aeb46 [email protected]     Ruth Hunter     10/29/2023 10:52:09 AM                   0
f343e332-b768-49c0-b946-dcc91a6f280a [email protected]      Ryan Smith      10/29/2023 10:52:11 AM                   0
7b2727b6-1d03-41e0-97ba-f5154d186f4f [email protected]    Sophie Lewis    10/29/2023 10:52:13 AM                   0
ac0b87d5-1647-40b9-9fb7-568270fe68e0 [email protected]        svd-adds        10/29/2023 10:52:15 AM                   0

获取具有特定域的 Microsoft 365 已删除用户

使用 PowerShell 查找具有特定域的 Microsoft 365 已删除用户。运行以下使用 Get-MgDirectoryDeletedItemAsUser cmdlet 的脚本。

第 5 行更改域名。

# Connect with privileges
Connect-MgGraph -Scopes "User.ReadWrite.All"

# Specify the domain name for filtering
$DomainName = "tajran.com"

# Get properties
$Properties = @(
    'Id',
    'userPrincipalName',
    'displayName',
    'deletedDateTime',
    'userType'
)

# Retrieve deleted directory items
$DeletedItems = Get-MgDirectoryDeletedItemAsUser -All -Property $Properties | Select-Object $Properties

# Check if there are no deleted accounts from the specified domain
$DomainSpecificDeletedItems = $DeletedItems | Where-Object { $_.userPrincipalName -like "*@$DomainName" }

if ($DomainSpecificDeletedItems.count -eq 0) {
    Write-Host "No deleted accounts found in the recycle bin for the domain $DomainName." -ForegroundColor Cyan
}
else {
    # Create an array to store the report
    $Report = @()

    # Loop through the deleted items and filter based on the domain name
    foreach ($Item in $DomainSpecificDeletedItems) {
        $DeletedDate = Get-Date($Item.DeletedDateTime)
        $DaysSinceDeletion = (New-TimeSpan $DeletedDate).Days

        # Create a custom object for each item and add it to the report
        $ReportLine = [PSCustomObject]@{
            Id                    = $Item.Id
            UserPrincipalName     = $Item.UserPrincipalName
            'Display Name'        = $Item.DisplayName
            Deleted               = $DeletedDate
            'Days Since Deletion' = $DaysSinceDeletion
            Type                  = $Item.UserType
        }
        $Report += $ReportLine
    }

    # Sort the report by 'Display Name'
    $Report | Sort-Object 'Display Name' | Format-Table
}

输出将仅显示具有该特定域的 Microsoft 365 已删除用户。

Id                                   UserPrincipalName                                       Display Name    Deleted                Days Since Deletion
--                                   -----------------                                       ------------    -------                -------------------
a02cb6da-5d88-469f-bfbf-ec78b5b4d1e8 [email protected]   Ali Tajran      10/29/2023 10:56:52 AM                   0

删除单个已删除的 Microsoft 365 用户

永久删除单个已删除的用户。

Remove-MgDirectoryDeletedItem -DirectoryObjectId "e09cb6da-5d88-469f-bfbf-ec78b5b4d1e8"

删除具有特定域的已删除的 Microsoft 365 用户

如果您想永久删除以特定域结尾的已删除 Microsoft 365 用户,可以按 UserPrincipalName 进行筛选。

更改第 5 行上的域。

# Connect with privileges
Connect-MgGraph -Scopes "User.ReadWrite.All"

# Specify the domain name for filtering
$DomainName = "tajran.com"

# Get properties
$Properties = @(
    'Id',
    'userPrincipalName',
    'displayName'
)

# Retrieve deleted directory items
$DeletedItems = Get-MgDirectoryDeletedItemAsUser -All -Property $Properties | Select-Object $Properties

# Check if there are no deleted accounts from the specified domain
$DomainSpecificDeletedItems = $DeletedItems | Where-Object { $_.userPrincipalName -like "*@$DomainName" }

if ($DomainSpecificDeletedItems.count -eq 0) {
    Write-Host "No deleted accounts found in the recycle bin for the domain $DomainName." -ForegroundColor Cyan
}
else {
    # Create an array to store the deleted user IDs
    $DeletedIds = @()

    # Loop through the deleted items
    foreach ($Item in $DomainSpecificDeletedItems) {
        $DeletedIds += $Item.Id

        # Display the name of the item being deleted
        Write-Host "Deleted $($Item.DisplayName)" -ForegroundColor Green

        # Hard delete the items
        Remove-MgDirectoryDeletedItem -DirectoryObjectId $Item.Id #-WhatIf
    }
}

删除所有已删除的 Microsoft 365 用户

运行以下 PowerShell 脚本以永久删除所有已删除的用户。

# Connect with privileges
Connect-MgGraph -Scopes "User.ReadWrite.All"

# Get properties
$Properties = @(
    'Id',
    'userPrincipalName',
    'displayName'
)

# Retrieve deleted directory items
$DeletedItems = Get-MgDirectoryDeletedItemAsUser -All -Property $Properties | Select-Object $Properties

# Check if there are no deleted accounts
if ($DeletedItems.Count -eq 0) {
    Write-Host "No deleted accounts found in the recycle bin." -ForegroundColor Cyan
}
else {
    # Create an array to store the deleted user IDs
    $DeletedIds = @()

    # Loop through the deleted items
    foreach ($Item in $DeletedItems) {
        $DeletedIds += $Item.Id
        
        # Display the name of the item being deleted
        Write-Host "Deleted $($Item.DisplayName)" -ForegroundColor Green

        # Hard delete the items
        Remove-MgDirectoryDeletedItem -DirectoryObjectId $Item.Id #-WhatIf
    }
}

运行上述命令后,我们可以验证已删除用户页面为空。

[玩转系统] 从 Microsoft 365 中永久删除用户

永久删除 Microsoft Entra 管理中心中的用户

要在 Microsoft Entra 管理中心中永久删除用户,请按照以下步骤操作:

  1. 登录 Microsoft Entra 管理中心
  2. 展开身份
  3. 点击用户 > 已删除的用户
  4. 选择用户
  5. 点击永久删除

[玩转系统] 从 Microsoft 365 中永久删除用户

  1. 所有用户都被永久删除

[玩转系统] 从 Microsoft 365 中永久删除用户

就是这样!这是否有助于您从回收站强制删除 Microsoft 365 用户?

了解更多:将 Microsoft 365 邮箱权限导出到 CSV »

结论

您了解了如何从 Microsoft 365 中永久删除用户。大多数情况下,您删除用户后,他们会自动移至 Microsoft Entra ID 回收站(软删除)。 30 天后,Microsoft 将从 Microsoft Entra ID 回收站中删除已删除的用户(硬删除)。下次您想要硬删除用户并且不想等待 30 天时,可以使用 PowerShell 或 Microsoft Entra 管理中心。

您喜欢这篇文章吗?您可能还喜欢将 Office 365 邮箱导出到 PST。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯