管理群组

本文介绍如何使用 Microsoft Entra PowerShell 管理Microsoft Entra ID中的组。 这些示例包括创建和更新组、添加用户和所有者、查询无所有者组和空组。

先决条件

创建组

若要创建组,请确保拥有创建组所需的权限。

Connect-Entra -Scopes 'Group.ReadWrite.All' 

若要创建新组,请运行以下命令。

$groupParams = @{
    DisplayName = 'Contoso marketing'
    MailEnabled = $false
    SecurityEnabled = $true
    MailNickName = 'NotSet'
}
New-EntraGroup @groupParams
DisplayName  Id                                   MailNickname Description GroupTypes
-----------  --                                   ------------ ----------- ----------
Contoso marketing aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb NotSet                   {}

此命令创建一个名称 Contoso marketing为的新组。

使用以下命令搜索创建的组。

Get-EntraGroup -Filter "displayName eq 'Contoso marketing'"
DisplayName        Id                                   MailNickname     Description        GroupTypes
-----------        --                                   ------------     -----------        ----------
Contoso marketing       aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb NotSet       Contoso marketing EMEA        {}

此命令返回新创建的组的详细信息。 还可以使用 GroupId (GUID) 搜索、更新或删除组。

更新组详情

通过运行以下命令更新组说明。

Get-EntraGroup -Filter "displayName eq 'Contoso marketing'" | Set-EntraGroup -Description 'Contoso marketing Global'

若要确认更新的说明,请再次运行 Get-EntraGroup

Get-EntraGroup -Filter "displayName eq 'Contoso marketing'"  

将用户添加到组

通过运行以下命令将用户添加到组。 是 GroupId 组 ID, MemberId 是用户 ID。 可以从Microsoft Entra 管理中心或通过运行 Get-EntraUser 命令获取用户 ID。

$group = Get-EntraGroup -Filter "displayName eq 'Contoso marketing'"
$user = Get-EntraUser -UserId 'SawyerM@contoso.com'
Add-EntraGroupMember -GroupId $group.Id -MemberId $user.Id

若要检索组成员,请使用以下命令:

$group = Get-EntraGroup -Filter "displayName eq 'Contoso marketing'"
Get-EntraGroup -GroupId $group.Id | Get-EntraGroupMember | Select-Object Id, DisplayName, '@odata.type' 
Id                                   DisplayName       @odata.type                     
------------------------------------ ----------------- -------------------------------
dddddddd-3333-4444-5555-eeeeeeeeeeee Sawyer Miller     #microsoft.graph.user
eeeeeeee-4444-5555-6666-ffffffffffff Alex Wilber       #microsoft.graph.user
aaaaaaaa-6666-7777-8888-bbbbbbbbbbbb My Application    #microsoft.graph.servicePrincipal
cccccccc-8888-9999-0000-dddddddddddd Contoso Group     #microsoft.graph.group

将用户添加为组所有者

通过运行以下命令将组所有者添加到组。 是 GroupId 组 ID, OwnerId 是用户 ID。

$group = Get-EntraGroup -Filter "displayName eq 'Contoso marketing'"
$owner = Get-EntraUser -UserId 'AdeleV@contoso.com'
Add-EntraGroupOwner -GroupId $group.Id -OwnerId $owner.Id

若要确认更新的组所有者,请使用以下命令:

$group = Get-EntraGroup -Filter "displayName eq 'Contoso marketing'"
Get-EntraGroup -GroupId $group.Id | Get-EntraGroupOwner | Select-Object Id, DisplayName, '@odata.type'
Id                                   DisplayName       @odata.type
------------------------------------ ----------------- ---------------------------
aaaaaaaa-6666-7777-8888-bbbbbbbbbbbb Adele Vance       #microsoft.graph.user

查询没有所有者的组或空组

若要查询没有所有者的组,请运行以下命令。

$allGroups = Get-EntraGroup -All
$groupsWithoutOwners = foreach ($group in $allGroups) {
    $owners = Get-EntraGroupOwner -GroupId $group.Id
    if ($owners.Count -eq 0) {
        $group
    }
}
$groupsWithoutOwners | Format-Table DisplayName, Id, GroupTypes
DisplayName              Id                                   GroupTypes
-----------              --                                   ----------
Contoso marketing        aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb {}
HelpDesk admin group     eeeeeeee-4444-5555-6666-ffffffffffff {}

若要查询不包含成员的组(空组),请运行以下命令。

$allGroups = Get-EntraGroup -All
$groupsWithoutMembers = foreach ($group in $allGroups) {
    $members = Get-EntraGroupMember -GroupId $group.Id
    if ($members.Count -eq 0) {
        $group
    }
}
$groupsWithoutMembers | Format-Table DisplayName, Id, GroupTypes
DisplayName             Id                                   GroupTypes
-----------            --                                   ----------
Contoso marketing      aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb {}
HelpDesk admin group   eeeeeeee-4444-5555-6666-ffffffffffff {}

清理资源

若要删除组,请使用以下命令:

Get-EntraGroup -Filter "displayName eq 'Contoso marketing'" | Remove-EntraGroup