导出 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 Excel) 打开 CSV 文件。
- 检查区块凭证柱子。
错误的表示该帐户已启用,并且真的表示该帐户已被禁用。
笔记:您想要使用 PowerShell 注销并禁用 Microsoft 365 用户吗?请阅读文章使用 PowerShell 在 Microsoft 365 中强制注销用户。
导出 Microsoft 365 禁用用户 PowerShell 脚本
Export-M365DisabledUsers.ps1 PowerShell 脚本将获取所有已禁用的 Microsoft 365 用户,将其输出到网格视图,并将其导出到 CSV 文件。
对于每个用户,它都会收集以下信息:
- ID
- 显示名称
- 用户主体名称
- 邮件
- 用户类型
- 账户启用
准备 Export-M365DisabledUsers PowerShell 脚本
上创建两个文件夹(中:)驾驶:
- 温度
- 脚本
下载 Export-M365DisabledUsers.ps1 PowerShell 脚本并将其放入C:脚本文件夹。该脚本会将 CSV 文件导出到C:温度文件夹。
另一种选择是将以下代码复制并粘贴到记事本中。给它起个名字导出-M365DisabledUsers.ps1并将其放置在C:脚本文件夹。
# Export path for CSV file
$csvPath = "C:TempDisabledUsers.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 禁用用户状态
对于单个用户,您可以使用下面的脚本。
运行脚本后,它会要求插入用户统一网络或者对象ID。
输出显示用户是否无法找到或可以找到。如果可以找到,输出将被发送到网格视图(网格视图)并导出到带有帐户状态(禁用/启用)的 CSV 文件。
# Specify the user UserPrincipalName or objectId
$userPrincipalName = Read-Host "Enter UPN or ObjectId"
# Export path for CSV file
$csvPath = "C:TempDisabledUser.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 -Force
重要的:在运行 cmdlet 或脚本之前,请务必更新到最新的 Microsoft Graph PowerShell 模块版本,以防止出现错误和不正确的结果。
运行连接-MgGraphcmdlet。
Connect-MgGraph -Scopes "User.Read.All"
运行 Export-M365DisabledUsers PowerShell 脚本
使用 PowerShell 获取所有非活动用户。运行以下命令来运行脚本 Export-M365DisabledUsers.ps1。
c:scripts.Export-M365DisabledUsers.ps1
网格视图
一个网格视图将显示包含所有禁用用户及其信息的列。
打开 Microsoft 365 禁用用户报告 CSV 文件
Export-M365DisabledUsers.ps1 PowerShell 脚本会将 Microsoft 365 用户不活动状态导出到 CSV 文件。找到文件禁用用户.csv在路径中C:温度。
使用您喜欢的应用程序打开 CSV 文件。在我们的示例中,它是 Microsoft Excel。
就是这样!
这是否可以帮助您将 Microsoft 365 禁用用户导出到 CSV 文件?
结论
您了解了如何从 Microsoft 365 导出禁用用户。PowerShell 或 Microsoft 365 管理中心非常适合快速检查帐户禁用状态。如果您想根据您的规范自定义报告,PowerShell 非常适合。
所有用户及其帐户状态(禁用/启用)的报告并在 Microsoft Excel 中进行过滤也是完美的。请记住在一段时间后删除禁用的用户。这取决于每个组织。
您喜欢这篇文章吗?您可能还喜欢使用 PowerShell 从 CSV 创建 Microsoft Entra ID 用户。不要忘记关注我们并分享这篇文章。
