Skip to content

Commit 989e806

Browse files
Add MemberObjectId parameter to Remove-PnPAzureADGroupMember and Remove-PnPAzureADGroupOwner cmdlets; update documentation and utility methods for handling directory objects (#5158)
* Add MemberObjectId parameter to Remove-PnPAzureADGroupMember and Remove-PnPAzureADGroupOwner cmdlets; update documentation and utility methods for handling directory objects * Update documentation/Remove-PnPAzureADGroupOwner.md Co-authored-by: Copilot <[email protected]> * Update documentation/Remove-PnPAzureADGroupMember.md Co-authored-by: Copilot <[email protected]> --------- Co-authored-by: Copilot <[email protected]>
1 parent c2c1b30 commit 989e806

File tree

5 files changed

+117
-9
lines changed

5 files changed

+117
-9
lines changed

documentation/Remove-PnPAzureADGroupMember.md

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ Removes members from a particular Azure Active Directory group. This can be a se
2020
## SYNTAX
2121

2222
```powershell
23-
Remove-PnPAzureADGroupMember -Identity <AzureADGroupPipeBind> -Users <String[]>
23+
Remove-PnPAzureADGroupMember -Identity <AzureADGroupPipeBind> -Users <String[]>
24+
```
25+
26+
```powershell
27+
Remove-PnPAzureADGroupMember -Identity <AzureADGroupPipeBind> -MemberObjectId <Guid[]>
2428
```
2529

2630
## DESCRIPTION
@@ -36,6 +40,22 @@ Remove-PnPAzureADGroupMember -Identity "Project Team" -Users "[email protected]
3640

3741
Removes the provided two users as members from the Azure Active Directory group named "Project Team"
3842

43+
### EXAMPLE 2
44+
```powershell
45+
# Remove a nested group by its ObjectId
46+
Remove-PnPAzureADGroupMember -Identity $parentGroupId -MemberObjectId $childGroupId
47+
```
48+
49+
Removes the group with ObjectId `$childGroupId` from the group identified by `$parentGroupId`.
50+
51+
### EXAMPLE 3
52+
```powershell
53+
# Pipeline by property name (Id)
54+
Get-PnPAzureADGroupMember -Identity $parentGroupId | Where-Object { $_.Id -eq $childGroupId } | Remove-PnPAzureADGroupMember -Identity $parentGroupId
55+
```
56+
57+
Pipes a member (group or user) whose `Id` matches `$childGroupId` into the cmdlet and removes it.
58+
3959
## PARAMETERS
4060

4161
### -Identity
@@ -66,6 +86,20 @@ Accept pipeline input: False
6686
Accept wildcard characters: False
6787
```
6888
89+
### -MemberObjectId
90+
The ObjectId(s) of directory object(s) (Users or Groups) to remove from the Azure Active Directory group. Use this to remove nested groups that do not have a UPN.
91+
92+
```yaml
93+
Type: Guid[]
94+
Parameter Sets: ByObjectId
95+
96+
Required: True
97+
Position: Named
98+
Default value: None
99+
Accept pipeline input: True (ByPropertyName)
100+
Accept wildcard characters: False
101+
```
102+
69103
## RELATED LINKS
70104
71105
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

documentation/Remove-PnPAzureADGroupOwner.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ Removes owners from a particular Azure Active Directory group. This can be a sec
2323
Remove-PnPAzureADGroupOwner -Identity <AzureADGroupPipeBind> -Users <String[]> [-Verbose]
2424
```
2525

26+
```powershell
27+
Remove-PnPAzureADGroupOwner -Identity <AzureADGroupPipeBind> -MemberObjectId <Guid[]> [-Verbose]
28+
```
29+
2630
## DESCRIPTION
2731

2832
Allows to remove owners from Azure Active Directory group.
@@ -36,6 +40,22 @@ Remove-PnPAzureADGroupOwner -Identity "Project Team" -Users "[email protected]
3640

3741
Removes the provided two users as owners from the Azure Active Directory group named "Project Team".
3842

43+
### EXAMPLE 2
44+
```powershell
45+
# Remove an owner by ObjectId
46+
Remove-PnPAzureADGroupOwner -Identity $groupId -MemberObjectId $ownerObjectId
47+
```
48+
49+
Removes the owner (user or group) with ObjectId `$ownerObjectId` from the group identified by `$groupId`.
50+
51+
### EXAMPLE 3
52+
```powershell
53+
# Pipeline by property name (Id)
54+
Get-PnPAzureADGroupOwner -Identity $groupId | Where-Object { $_.Id -eq $ownerObjectId } | Remove-PnPAzureADGroupOwner -Identity $groupId
55+
```
56+
57+
Pipes an owner whose `Id` matches `$ownerObjectId` into the cmdlet and removes it.
58+
3959
## PARAMETERS
4060

4161
### -Identity
@@ -66,6 +86,20 @@ Accept pipeline input: False
6686
Accept wildcard characters: False
6787
```
6888
89+
### -MemberObjectId
90+
The ObjectId(s) of directory object(s) (Users or Groups) to remove from the Azure Active Directory group as owners. Use this to remove owners that do not have a UPN.
91+
92+
```yaml
93+
Type: Guid[]
94+
Parameter Sets: ByObjectId
95+
96+
Required: True
97+
Position: Named
98+
Default value: None
99+
Accept pipeline input: True (ByPropertyName)
100+
Accept wildcard characters: False
101+
```
102+
69103
## RELATED LINKS
70104
71105
[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)

src/Commands/AzureAD/RemoveAzureADGroupMember.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77

88
namespace PnP.PowerShell.Commands.Graph
99
{
10-
[Cmdlet(VerbsCommon.Remove, "PnPAzureADGroupMember")]
10+
[Cmdlet(VerbsCommon.Remove, "PnPAzureADGroupMember", DefaultParameterSetName = "ByUPN")]
1111
[RequiredApiDelegatedOrApplicationPermissions("graph/Group.ReadWrite.All")]
1212
[Alias("Remove-PnPEntraIDGroupMember")]
1313
public class RemoveAzureADGroupMember : PnPGraphCmdlet
1414
{
15-
[Parameter(Mandatory = true, ValueFromPipeline = true)]
15+
[Parameter(Mandatory = true, ValueFromPipeline = true, ParameterSetName = "ByUPN")]
16+
[Parameter(Mandatory = true, ValueFromPipeline = true, ParameterSetName = "ByObjectId")]
1617
public AzureADGroupPipeBind Identity;
1718

18-
[Parameter(Mandatory = true)]
19+
[Parameter(Mandatory = true, ParameterSetName = "ByUPN")]
1920
public string[] Users;
2021

22+
[Parameter(Mandatory = true, ParameterSetName = "ByObjectId", ValueFromPipelineByPropertyName = true)]
23+
[Alias("ObjectId", "Id")]
24+
public System.Guid[] MemberObjectId;
25+
2126
protected override void ExecuteCmdlet()
2227
{
2328
Group group = null;
@@ -29,7 +34,14 @@ protected override void ExecuteCmdlet()
2934

3035
if (group != null)
3136
{
32-
Microsoft365GroupsUtility.RemoveMembers(GraphRequestHelper, new System.Guid(group.Id), Users);
37+
if (ParameterSetName == "ByUPN" && Users != null && Users.Length > 0)
38+
{
39+
Microsoft365GroupsUtility.RemoveMembers(GraphRequestHelper, new System.Guid(group.Id), Users);
40+
}
41+
else if (ParameterSetName == "ByObjectId" && MemberObjectId != null && MemberObjectId.Length > 0)
42+
{
43+
Microsoft365GroupsUtility.RemoveDirectoryMembers(GraphRequestHelper, new System.Guid(group.Id), MemberObjectId);
44+
}
3345
}
3446
}
3547
}

src/Commands/AzureAD/RemoveAzureADGroupOwner.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,22 @@
77

88
namespace PnP.PowerShell.Commands.Graph
99
{
10-
[Cmdlet(VerbsCommon.Remove, "PnPAzureADGroupOwner")]
10+
[Cmdlet(VerbsCommon.Remove, "PnPAzureADGroupOwner", DefaultParameterSetName = "ByUPN")]
1111
[RequiredApiDelegatedOrApplicationPermissions("graph/Group.ReadWrite.All")]
1212
[Alias("Remove-PnPEntraIDGroupOwner")]
1313
public class RemoveAzureADGroupOwner : PnPGraphCmdlet
1414
{
15-
[Parameter(Mandatory = true, ValueFromPipeline = true)]
15+
[Parameter(Mandatory = true, ValueFromPipeline = true, ParameterSetName = "ByUPN")]
16+
[Parameter(Mandatory = true, ValueFromPipeline = true, ParameterSetName = "ByObjectId")]
1617
public AzureADGroupPipeBind Identity;
1718

18-
[Parameter(Mandatory = true)]
19+
[Parameter(Mandatory = true, ParameterSetName = "ByUPN")]
1920
public string[] Users;
2021

22+
[Parameter(Mandatory = true, ParameterSetName = "ByObjectId", ValueFromPipelineByPropertyName = true)]
23+
[Alias("ObjectId", "Id")]
24+
public System.Guid[] MemberObjectId;
25+
2126
protected override void ExecuteCmdlet()
2227
{
2328
Group group = null;
@@ -29,7 +34,14 @@ protected override void ExecuteCmdlet()
2934

3035
if (group != null)
3136
{
32-
Microsoft365GroupsUtility.RemoveOwners(GraphRequestHelper, new System.Guid(group.Id), Users);
37+
if (ParameterSetName == "ByUPN" && Users != null && Users.Length > 0)
38+
{
39+
Microsoft365GroupsUtility.RemoveOwners(GraphRequestHelper, new System.Guid(group.Id), Users);
40+
}
41+
else if (ParameterSetName == "ByObjectId" && MemberObjectId != null && MemberObjectId.Length > 0)
42+
{
43+
Microsoft365GroupsUtility.RemoveDirectoryOwners(GraphRequestHelper, new System.Guid(group.Id), MemberObjectId);
44+
}
3345
}
3446
}
3547
}

src/Commands/Utilities/Microsoft365GroupsUtility.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,22 @@ internal static void RemoveMembers(ApiRequestHelper requestHelper, Guid groupId,
362362
RemoveUserFromGroup(requestHelper, "members", groupId, users);
363363
}
364364

365+
internal static void RemoveDirectoryMembers(ApiRequestHelper requestHelper, Guid groupId, Guid[] directoryObjects)
366+
{
367+
foreach (var dirObject in directoryObjects)
368+
{
369+
requestHelper.Delete($"v1.0/groups/{groupId}/members/{dirObject}/$ref");
370+
}
371+
}
372+
373+
internal static void RemoveDirectoryOwners(ApiRequestHelper requestHelper, Guid groupId, Guid[] directoryObjects)
374+
{
375+
foreach (var dirObject in directoryObjects)
376+
{
377+
requestHelper.Delete($"v1.0/groups/{groupId}/owners/{dirObject}/$ref");
378+
}
379+
}
380+
365381
private static void RemoveUserFromGroup(ApiRequestHelper requestHelper, string groupName, Guid groupId, string[] users)
366382
{
367383
foreach (var user in users)

0 commit comments

Comments
 (0)