使用 PowerShell 管理安全組

本文適用於 Microsoft 365 企業版和 Office 365 企業版。

您可以使用 PowerShell for Microsoft 365 作為管理安全組 Microsoft 365 系統管理中心 的替代方案。

本文說明列出、建立、變更設定,以及移除安全組。

當本文中的命令區塊要求您指定變數值時,請使用這些步驟。

  1. 將命令區塊複製到剪貼簿,並貼到記事本或PowerShell整合式腳本環境 (ISE) 。
  2. 填入變數值,並移除 「<」 和 「>字元」。
  3. 在 PowerShell 視窗或 PowerShell ISE 中執行命令。

請參閱 維護安全組成員資格 以使用PowerShell管理群組成員資格。

使用 Microsoft Graph PowerShell 管理安全組

注意事項

Azure Active Directory 模組正由 Microsoft Graph PowerShell SDK 取代。 您可以使用 Microsoft Graph PowerShell SDK 來存取所有的 Microsoft Graph API。 如需詳細資訊,請參閱 開始使用 Microsoft Graph PowerShell SDK

首先, 聯機到您的 Microsoft 365 租使用者

管理安全組需要 Group.ReadWrite.All 許可權範圍或 [列出 subscribedSkus] 參考頁面中所列的其中一個其他許可權 圖形 API。 本文中的某些命令可能需要不同的許可權範圍,在此情況下,這會在相關章節中註明。

Connect-Graph -Scopes Group.ReadWrite.All

列出您的群組

使用此命令來列出所有群組。

Get-MgGroup -All

使用這些命令,依其顯示名稱顯示特定群組的設定。

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

建立新的群組

使用此命令來建立新的安全組。

Connect-MgGraph -Scopes "Group.Create"
New-MgGroup -Description "<group purpose>" -DisplayName "<name>" -MailEnabled:$false -SecurityEnabled -MailNickname "<email name>"

顯示群組的設定

使用這些命令顯示群組的設定。

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

拿掉安全組

使用這些命令來移除安全組。

$groupName="<display name of the group>"
$group = Get-MgGroup -Filter "displayName eq '$groupName'"
Remove-MgGroup -GroupId $group.Id

管理安全組的擁有者

使用這些命令來顯示安全組的目前擁有者。

$groupName="<display name of the group>"

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "GroupMember.Read.All"

# Display group owners
Get-MgGroupOwner -GroupId (Get-MgGroup | Where-Object { $_.DisplayName -eq $groupName }).Id

使用這些命令,將使用者帳戶的 用戶主體名稱 (UPN) 新增至安全組的目前擁有者。

$userUPN="<UPN of the user account to add>"
$groupName="<display name of the group>"

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All", "User.ReadBasic.All"

# Get the group and user
$group = Get-MgGroup -Filter "displayName eq '$groupName'"
$userId = (Get-MgUser -Filter "userPrincipalName eq '$userUPN'").Id

# Add the user as an owner to the group
$newGroupOwner =@{
  "@odata.id"= "https://graph.microsoft.com/v1.0/users/$userId"
  }

New-MgGroupOwnerByRef -GroupId $group.Id -BodyParameter $newGroupOwner

使用這些命令,將用戶帳戶依 其顯示名稱 新增至安全組的目前擁有者。

$userName="<Display name of the user account to add>"
$groupName="<display name of the group>"

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All", "Directory.Read.All", "User.ReadBasic.All"

# Get the group and user
$group = Get-MgGroup -All | Where-Object { $_.DisplayName -eq $groupName }
$userId = (Get-MgUser -All | Where-Object { $_.DisplayName -eq $userName }).Id

# Add the user as an owner to the group
$newGroupOwner =@{
  "@odata.id"= "https://graph.microsoft.com/v1.0/users/$userId"
  }

New-MgGroupOwnerByRef -GroupId $group.Id -BodyParameter $newGroupOwner

使用這些命令,透過其 UPN 從安全組的目前擁有者移除用戶帳戶。

$userUPN="<UPN of the user account to remove>"
$groupName="<display name of the group>"

# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All", "Directory.ReadWrite.All"

# Get the group and user
$group = Get-MgGroup -Filter "displayName eq '$groupName'" | Select-Object -First 1
$user = Get-MgUser -Filter "userPrincipalName eq '$userUPN'" | Select-Object -First 1

# Remove the user from the group
Remove-MgGroupOwnerByRef -GroupId $group.Id -DirectoryObjectId $user.Id

使用這些命令,從安全組的目前擁有者中,依 其顯示名稱 移除用戶帳戶。

$userName="<Display name of the user account to remove>"
$groupName="<display name of the group>"
$group = Get-MgGroup | Where-Object { $_.DisplayName -eq $groupName }
$user = Get-MgUser | Where-Object { $_.DisplayName -eq $userName }

Remove-MgGroupOwnerByRef -GroupId $group.Id -DirectoryObjectId $user.Id

另請參閱

以 PowerShell 管理 Microsoft 365 使用者帳戶、授權和群組

使用 PowerShell 管理 Microsoft 365

開始使用適用於 Microsoft 365 的 PowerShell