使用 PowerShell 阻止 Microsoft 365 用户帐户
此文章适用于 Microsoft 365 企业版和 Office 365 企业版。
阻止访问 Microsoft 365 帐户时,会阻止任何人使用该帐户登录并访问 Microsoft 365 组织中的服务和数据。 可以使用 PowerShell 阻止对单个或多个用户帐户的访问。
使用用于图表模块的 Azure Active Directory PowerShell
首先, 连接到 Microsoft 365 租户。
阻止对单个用户帐户的访问
使用以下语法阻止单个用户帐户:
Set-AzureADUser -ObjectID <sign-in name of the user account> -AccountEnabled $false
注意
Set-AzureAD cmdlet 中的 -ObjectID 参数接受帐户登录名(也称为用户主体名称)或帐户的对象 ID。
此示例阻止访问用户帐户 fabricec@litwareinc.com。
Set-AzureADUser -ObjectID fabricec@litwareinc.com -AccountEnabled $false
若要取消阻止此用户帐户,请运行以下命令:
Set-AzureADUser -ObjectID fabricec@litwareinc.com -AccountEnabled $true
若要根据用户的显示名称显示用户帐户 UPN,请使用以下命令:
$userName="<display name>"
Write-Host (Get-AzureADUser | where {$_.DisplayName -eq $userName}).UserPrincipalName
此示例显示用户 Caleb Sills 的用户帐户 UPN。
$userName="Caleb Sills"
Write-Host (Get-AzureADUser | where {$_.DisplayName -eq $userName}).UserPrincipalName
若要基于用户的显示名称阻止帐户,请使用以下命令:
$userName="<display name>"
Set-AzureADUser -ObjectID (Get-AzureADUser | where {$_.DisplayName -eq $userName}).UserPrincipalName -AccountEnabled $false
若要检查用户帐户的阻止状态,请使用以下命令:
Get-AzureADUser -ObjectID <UPN of user account> | Select DisplayName,AccountEnabled
阻止多个用户帐户
若要阻止对多个用户帐户的访问,请在每行创建一个包含一个帐户登录名的文本文件,如下所示:
akol@contoso.com
tjohnston@contoso.com
kakers@contoso.com
在以下命令中,示例文本文件为 C:\My Documents\Accounts.txt。 将此文件名替换为文本文件的路径和文件名。
若要阻止访问该文本文件中列出的帐户,请运行以下命令:
Get-Content "C:\My Documents\Accounts.txt" | ForEach {Set-AzureADUser -ObjectID $_ -AccountEnabled $false}
若要取消阻止文本文件中列出的帐户,请运行以下命令:
Get-Content "C:\My Documents\Accounts.txt" | ForEach {Set-AzureADUser -ObjectID $_ -AccountEnabled $true}
使用Microsoft Azure Active Directory模块进行Windows PowerShell
首先, 连接到 Microsoft 365 租户。
阻止单个用户帐户
使用以下语法阻止对单个用户帐户的访问:
Set-MsolUser -UserPrincipalName <sign-in name of user account> -BlockCredential $true
注意
对于名称中具有 Msol 的Windows PowerShell模块和 cmdlet,PowerShell Core 不支持 Microsoft Azure Active Directory 模块。 必须从 Windows PowerShell 运行这些 cmdlet。
此示例阻止访问用户帐户 fabricec@litwareinc.com。
Set-MsolUser -UserPrincipalName fabricec@litwareinc.com -BlockCredential $true
若要取消阻止该用户帐户,请运行以下命令:
Set-MsolUser -UserPrincipalName <sign-in name of user account> -BlockCredential $false
若要检查用户帐户的阻止状态,请运行以下命令:
Get-MsolUser -UserPrincipalName <sign-in name of user account> | Select DisplayName,BlockCredential
阻止对多个用户帐户的访问
首先,创建一个文本文件,其中每行包含一个帐户,如下所示:
akol@contoso.com
tjohnston@contoso.com
kakers@contoso.com
在以下命令中,示例文本文件为 C:\My Documents\Accounts.txt。 将此文件名替换为文本文件的路径和文件名。
若要阻止对文本文件中列出的帐户的访问,请运行以下命令:
Get-Content "C:\My Documents\Accounts.txt" | ForEach { Set-MsolUser -UserPrincipalName $_ -BlockCredential $true }
若要解除阻止该文本文件中列出的帐户,请运行以下命令:
Get-Content "C:\My Documents\Accounts.txt" | ForEach { Set-MsolUser -UserPrincipalName $_ -BlockCredential $false }
另请参阅
使用 PowerShell 管理 Microsoft 365 用户帐户、许可证和组
反馈
提交和查看相关反馈