使用 PowerShell 维护安全组成员身份

此文章适用于 Microsoft 365 企业版和 Office 365 企业版。

可以使用 PowerShell for Microsoft 365 作为Microsoft 365 管理中心的替代方法,以维护 Microsoft 365 中的安全组成员身份。

使用用于图表模块的 Azure Active Directory PowerShell

首先, 连接到 Microsoft 365 租户

添加或删除作为组成员的用户帐户

注意

自 2024 年 3 月 30 日起,Azure AD 和 MSOnline PowerShell 模块已弃用。 若要了解详细信息,请阅读 弃用更新。 在此日期之后,对这些模块的支持仅限于 Microsoft Graph PowerShell SDK 和安全修补程序的迁移帮助。 弃用的模块将继续运行到 2025 年 3 月 30 日。

建议迁移到 Microsoft Graph PowerShell,以便与以前为 Azure AD) Microsoft Entra ID (交互。 有关常见的迁移问题,请参阅 迁移常见问题解答注意: MSOnline 1.0.x 版可能会在 2024 年 6 月 30 日之后遇到中断。

若要按 UPN 添加用户帐户,请填写用户帐户“用户名” (UPN) (示例: belindan@contoso.com) 和安全组显示名称,删除“”<和“>”字符,并在 PowerShell 窗口或 PowerShell 集成脚本环境 (ISE) 中运行这些命令。

$userUPN="<UPN of the user account to add>"
$groupName="<display name of the group>"
Add-AzureADGroupMember -RefObjectId (Get-AzureADUser | Where { $_.UserPrincipalName -eq $userUPN }).ObjectID -ObjectId (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

若要按其显示名称添加用户帐户,请填写用户帐户显示名称 (示例:Belinda Newman) 和组显示名称,并在 PowerShell 窗口或 PowerShell ISE 中运行以下命令。

$userName="<display name of the user account to add>"
$groupName="<display name of the group>"
Add-AzureADGroupMember -RefObjectId (Get-AzureADUser | Where { $_.DisplayName -eq $userName }).ObjectID -ObjectId (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

若要按 UPN 删除用户帐户,请填写用户帐户 UPN (示例: belindan@contoso.com) 和组显示名称,并在 PowerShell 窗口或 PowerShell ISE 中运行以下命令。

$userUPN="<UPN of the user account to remove>"
$groupName="<display name of the group>"
Remove-AzureADGroupMember -MemberId (Get-AzureADUser | Where { $_.UserPrincipalName -eq $userUPN }).ObjectID -ObjectID (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

若要按显示名称删除用户帐户,请填写用户帐户显示名称 (示例:Belinda Newman) 和组显示名称,并在 PowerShell 窗口或 PowerShell ISE 中运行以下命令。

$userName="<display name of the user account to remove>"
$groupName="<display name of the group>"
Remove-AzureADGroupMember -MemberId (Get-AzureADUser | Where { $_.DisplayName -eq $userName }).ObjectID -ObjectID (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

添加或删除组作为组的成员

安全组可以包含其他组作为成员。 但是,Microsoft 365 组不能。 本部分包含 PowerShell 命令,用于仅添加或删除安全组的组。

若要按组的显示名称添加组,请填写要添加的组的显示名称以及将包含成员组的组的显示名称,并在 PowerShell 窗口或 PowerShell ISE 中运行这些命令。

$groupMemberName="<display name of the group to add>"
$groupName="<display name of the group that will contain the member group>"
Add-AzureADGroupMember -RefObjectId (Get-AzureADGroup | Where { $_.DisplayName -eq $groupMemberName }).ObjectID -ObjectID (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

若要按组的显示名称删除组,请填写要删除的组的显示名称以及将包含成员组的组的显示名称,并在 PowerShell 窗口或 PowerShell ISE 中运行这些命令。

$groupMemberName="<display name of the group to add>"
$groupName="<display name of the group that will contain the member group>"
Remove-AzureADGroupMember -MemberId (Get-AzureADGroup | Where { $_.DisplayName -eq $groupMemberName }).ObjectID -ObjectID (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

使用Microsoft Azure Active Directory模块进行Windows PowerShell

首先, 连接到 Microsoft 365 租户

添加或删除作为组成员的用户帐户

若要按 UPN 添加用户帐户,请填写用户帐户“用户主体名称” (UPN) (示例: belindan@contoso.com) 和组显示名称,删除“”<和“>”字符,并在 PowerShell 窗口或 PowerShell ISE 中运行这些命令。

$userUPN="<UPN of the user account to add>"
$groupName="<display name of the group>"
Add-MsolGroupMember -GroupMemberObjectId (Get-MsolUser | Where { $_.UserPrincipalName -eq $userUPN }).ObjectID -GroupObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

若要按其显示名称添加用户帐户,请填写用户帐户显示名称 (示例:Belinda Newman) 和组显示名称,并在 PowerShell 窗口或 PowerShell ISE 中运行以下命令。

$userName="<display name of the user account to add>"
$groupName="<display name of the group>"
Add-MsolGroupMember -GroupMemberObjectId (Get-MsolUser | Where { $_.DisplayName -eq $userName }).ObjectID -GroupObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

若要按 UPN 删除用户帐户,请填写用户帐户 UPN (示例: belindan@contoso.com) 和组显示名称,并在 PowerShell 窗口或 PowerShell ISE 中运行以下命令。

$userUPN="<UPN of the user account to remove>"
$groupName="<display name of the group>"
Remove-MsolGroupMember -GroupMemberObjectId (Get-MsolUser | Where { $_.UserPrincipalName -eq $userUPN }).ObjectID -GroupObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

若要按显示名称删除用户帐户,请填写用户帐户显示名称 (示例:Belinda Newman) 和组显示名称,并在 PowerShell 窗口或 PowerShell ISE 中运行以下命令。

$userName="<display name of the user account to remove>"
$groupName="<display name of the group>"
Remove-MsolGroupMember -GroupMemberObjectId (Get-MsolUser | Where { $_.DisplayName -eq $userName }).ObjectID -GroupObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

添加或删除组作为组的成员

安全组可以包含其他组作为成员。 但是,Microsoft 365 组不能。 本部分包含 PowerShell 命令,用于仅添加或删除安全组的组。

若要按组的显示名称添加组,请填写要添加的组的显示名称以及将包含成员组的组的显示名称,并在 PowerShell 窗口或 PowerShell ISE 中运行这些命令。

$groupMemberName="<display name of the group to add>"
$groupName="<display name of the group that will contain the member group>"
Add-MsolGroupMember -GroupMemberObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupMemberName }).ObjectID -GroupObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupName }).ObjectID -GroupMemberType Group

若要按组的显示名称删除组,请填写要删除的组的显示名称以及将包含成员组的组的显示名称,并在 PowerShell 窗口或 PowerShell ISE 中运行这些命令。

$groupMemberName="<display name of the group to add>"
$groupName="<display name of the group contains the member group>"
Remove-MsolGroupMember -GroupMemberObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupMemberName }).ObjectID -GroupObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupName }).ObjectID -GroupMemberType Group

另请参阅

使用 PowerShell 管理 Microsoft 365 用户帐户、许可证和组

使用 PowerShell 管理 Microsoft 365

PowerShell for Microsoft 365 入门