使用 PowerShell 管理安全组

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

可以使用适用于 Microsoft 365 的 PowerShell 作为管理安全组Microsoft 365 管理中心的替代方法。

本文介绍列出、创建、更改设置和删除安全组。

如果本文中的命令块要求指定变量值,请使用以下步骤。

  1. 将命令块复制到剪贴板,并将其粘贴到记事本或 PowerShell 集成脚本环境 (ISE) 。
  2. 填写变量值并删除“”<和“>”字符。
  3. 在 PowerShell 窗口或 PowerShell ISE 中运行命令。

请参阅 维护安全组成员身份 以使用 PowerShell 管理组成员身份。

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

首先, 连接到 Microsoft 365 租户

列出组

使用此命令列出所有组。

Get-AzureADGroup

使用这些命令可按特定组的显示名称显示其设置。

$groupName="<display name of the group>"
Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }

创建新组

使用此命令创建新的安全组。

New-AzureADGroup -Description "<group purpose>" -DisplayName "<name>" -MailEnabled $false -SecurityEnabled $true -MailNickName "<email name>"

更改组的设置

使用以下命令显示组的设置。

$groupName="<display name of the group>"
Get-AzureADGroup | Where { $_.DisplayName -eq $groupName } | Select *

然后,使用 Set-AzureADGroup 一文确定如何更改设置。

删除安全组

使用以下命令删除安全组。

$groupName="<display name of the group>"
Remove-AzureADGroup -ObjectId (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectId

管理安全组的所有者

使用以下命令可显示安全组的当前所有者。

$groupName="<display name of the group>"
Get-AzureADGroupOwner -ObjectId (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectId

使用这些命令将用户帐户的用户 主体名称 (UPN) 添加到安全组的当前所有者。

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

使用这些命令将用户帐户的 显示名称 添加到安全组的当前所有者。

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

使用这些命令通过 UPN 删除用户帐户,以便安全组的当前所有者。

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

使用这些命令可按用户帐户的 显示名称 删除安全组的当前所有者。

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

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

首先, 连接到 Microsoft 365 租户

列出组

使用此命令列出所有组。

Get-MsolGroup

使用这些命令可按特定组的显示名称显示其设置。

$groupName="<display name of the group>"
Get-MsolGroup | Where { $_.DisplayName -eq $groupName }

创建新组

使用此命令创建新的安全组。

New-MsolGroup -Description "<group purpose>" -DisplayName "<name>"

更改组的设置

使用以下命令显示组的设置。

$groupName="<display name of the group>"
Get-MsolGroup | Where { $_.DisplayName -eq $groupName } | Select *

然后,使用 Set-MsolGroup 一文确定如何更改设置。

删除安全组

使用以下命令删除安全组。

$groupName="<display name of the group>"
Remove-MsolGroup -ObjectId (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectId

另请参阅

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

使用 PowerShell 管理 Microsoft 365

PowerShell for Microsoft 365 入门