[玩转系统] 从 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 用户的一个绝佳方法是使用 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 PowerShell 和 Microsoft Graph Beta PowerShell 模块。这是因为某些 cmdlet 在最终版本中尚不可用,并且无法运行。在运行 cmdlet 或脚本之前将两个模块更新到最新版本,以防止出现错误和不正确的结果。
运行 Connect-MgGraph cmdlet 以启动与 Microsoft Entra ID 的连接。
Connect-MgGraph -Scopes "User.ReadWrite.All"
运行上述 cmdlet 后,将显示登录窗口。填写凭据并登录。
使用 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 Entra 管理中心中的用户
要在 Microsoft Entra 管理中心中永久删除用户,请按照以下步骤操作:
- 登录 Microsoft Entra 管理中心
- 展开身份
- 点击用户 > 已删除的用户
- 选择用户
- 点击永久删除
- 所有用户都被永久删除
就是这样!这是否有助于您从回收站强制删除 Microsoft 365 用户?
了解更多:将 Microsoft 365 邮箱权限导出到 CSV »
结论
您了解了如何从 Microsoft 365 中永久删除用户。大多数情况下,您删除用户后,他们会自动移至 Microsoft Entra ID 回收站(软删除)。 30 天后,Microsoft 将从 Microsoft Entra ID 回收站中删除已删除的用户(硬删除)。下次您想要硬删除用户并且不想等待 30 天时,可以使用 PowerShell 或 Microsoft Entra 管理中心。
您喜欢这篇文章吗?您可能还喜欢将 Office 365 邮箱导出到 PST。不要忘记关注我们并分享这篇文章。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag