导出 Microsoft 365 禁用用户报告
您想要列出 Microsoft 365 中所有已禁用的 Microsoft 365 帐户。通过 Microsoft Entra 检查这一点需要时间。使用 Microsoft Graph PowerShell 可以更快地检索所有禁用的用户。在本文中,您将了解如何导出 Microsoft 365 禁用用户。
在 Microsoft 365 管理中心查找禁用用户
若要在 Microsoft 365 管理中心检查 Microsoft 365 用户禁用状态,请按照以下步骤操作:
登录 Microsoft 365 管理中心。
展开用户。
点击活跃用户。
-
选择用户。
出现登录被阻止,这意味着该帐户已被禁用。如果您没有看到任何内容,则该帐户已启用。
为一个用户执行此操作就可以了。但如果您想检查多个用户,则更容易获得包含所有用户及其状态的报告。让我们在下一步中看看。
在 Microsoft 365 管理中心导出禁用用户报告
若要从 Microsoft 365 管理中心下载 Microsoft 365 用户状态和更多信息,请按照以下步骤操作:
登录 Microsoft 365 管理中心。
展开用户。
单击活动用户 > … > 导出用户。
-
使用您喜欢的程序 (Microsoft Excel) 打开 CSV 文件。
检查阻止凭据列。
False 表示帐户已启用,True 表示帐户已禁用。
注意:您想要使用 PowerShell 注销并禁用 Microsoft 365 用户吗?阅读文章使用 PowerShell 在 Microsoft 365 中强制注销用户。
导出 Microsoft 365 禁用用户 PowerShell 脚本
Export-M365DisabledUsers.ps1 PowerShell 脚本将获取所有已禁用的 Microsoft 365 用户,将其输出到网格视图,并将其导出到 CSV 文件。
对于每个用户,它都会收集以下信息:
ID
显示名称
用户主体名称
邮件
-
用户类型
账户启用
准备 Export-M365DisabledUsers PowerShell 脚本
在(C:)驱动器上创建两个文件夹:
温度
脚本
下载 Export-M365DisabledUsers.ps1 PowerShell 脚本并将其放置在 C:\scripts 文件夹中。该脚本会将 CSV 文件导出到 C:\temp 文件夹。
另一种选择是将以下代码复制并粘贴到记事本中。将其命名为 Export-M365DisabledUsers.ps1 并将其放置在 C:\scripts 文件夹中。
# Export path for CSV file
$csvPath = "C:\Temp\DisabledUsers.csv"
# Define the properties to retrieve from the user
$Properties = @(
'Id',
'DisplayName',
'UserPrincipalName',
'Mail',
'UserType',
'AccountEnabled'
)
# Connect to the Microsoft Graph API
Connect-MgGraph -Scopes "User.Read.All"
# Get a list of inactive users from the Microsoft Graph API
$disabledUsers = Get-MgUser -All -Filter "AccountEnabled eq false" -Property $Properties |
Select-Object $Properties |
Sort-Object -Property UserPrincipalName
# Display inactive users data in a graphical grid view
$disabledUsers | Out-GridView -Title "Disabled users"
# Export inactive users data to CSV file
try {
$disabledUsers | Export-Csv -Path $csvPath -NoTypeInformation -Encoding UTF8
Write-Host "Script completed. Results exported to $csvPath." -ForegroundColor Cyan
}
catch {
Write-Host "An error occurred while exporting data to CSV: $_" -ForegroundColor Red
}第 2 行:编辑 CSV 文件路径
使用 PowerShell 脚本检查单个 Microsoft 365 禁用用户状态
对于单个用户,您可以使用以下脚本。
运行脚本后,它会要求插入用户 UPN 或 ObjectID。
输出显示用户是否无法找到或可以找到。如果可以找到,输出将发送到网格视图 (Out-GridView) 并导出到带有帐户状态(禁用/启用)的 CSV 文件。
# Specify the user UserPrincipalName or objectId
$userPrincipalName = Read-Host "Enter UPN or ObjectId"
# Export path for CSV file
$csvPath = "C:\Temp\DisabledUser.csv"
# Connect to the Microsoft Graph API
Connect-MgGraph -Scopes "User.Read.All"
try {
# Define the properties to retrieve from the user
$Properties = @(
'Id',
'DisplayName',
'Mail',
'UserPrincipalName',
'UserType',
'AccountEnabled'
)
# Get the user object based on the provided userPrincipalName
$user = Get-MgUser -Filter "UserPrincipalName eq '$userPrincipalName'" -Property $Properties |
Select-Object -Property $Properties
if ($user) {
# Export the data for the specified user to CSV file
$user | Export-Csv -Path $csvPath -NoTypeInformation -Encoding UTF8
# Display the data in Out-GridView
$user | Out-GridView -Title "Disabled users"
Write-Host "Script completed. Results exported to $csvPath." -ForegroundColor Cyan
}
else {
Write-Host "The specified user does not exist." -ForegroundColor Yellow
}
}
catch {
# Output the error message
Write-Host "An error occurred while exporting data to CSV: $_" -ForegroundColor Red
# Display the data in Out-GridView without exporting to CSV
$user | Out-GridView -Title "Disabled users"
}第 5 行:编辑 CSV 文件路径
连接到 Microsoft Graph PowerShell
在我们进一步继续并获取所有用户的非活动状态之前,我们需要安装并连接到 Microsoft Graph PowerShell。
以管理员身份启动 Windows PowerShell 并运行以下命令。
Install-Module Microsoft.Graph -ForceInstall-Module Microsoft.Graph.Beta -AllowClobber -Force重要提示:始终安装 Microsoft Graph PowerShell 和 Microsoft Graph Beta PowerShell 模块。这是因为某些 cmdlet 在最终版本中尚不可用,并且无法运行。在运行 cmdlet 或脚本之前将两个模块更新到最新版本,以防止出现错误和不正确的结果。
运行 Connect-MgGraph cmdlet。
Connect-MgGraph -Scopes "User.Read.All"运行 Export-M365DisabledUsers PowerShell 脚本
使用 PowerShell 获取所有非活动用户。运行以下命令以运行脚本 Export-M365DisabledUsers.ps1。
c:\scripts\.\Export-M365DisabledUsers.ps1网格视图
Out-GridView 将显示包含所有禁用用户及其信息的列。
打开 Microsoft 365 禁用用户报告 CSV 文件
Export-M365DisabledUsers.ps1 PowerShell 脚本会将 Microsft 365 用户不活动情况导出到 CSV 文件。在路径 C:\temp 中找到文件 DisabledUsers.csv。
使用您喜欢的应用程序打开 CSV 文件。在我们的示例中,它是 Microsoft Excel。
就是这样!
这是否可以帮助您将 Microsoft 365 禁用用户导出到 CSV 文件?
了解更多:从 Active Directory 导出禁用的用户 »
结论
您了解了如何从 Microsoft 365 导出禁用用户。PowerShell 或 Microsoft 365 管理中心非常适合快速检查帐户禁用状态。如果您想根据您的规范自定义报告,PowerShell 非常适合。
所有用户及其帐户状态(禁用/启用)的报告并在 Microsoft Excel 中进行过滤也是完美的。请记住在一段时间后删除禁用的用户。这取决于每个组织。
您喜欢这篇文章吗?您可能还喜欢使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户。不要忘记关注我们并分享这篇文章。






