user6314597
user6314597

Reputation:

PowerShell - Remove-ADGroup not allowing pipe-in from Get-ADGroup -filter

I have the following line of code, which is supposed to get all Active Directory groups beginning with the @ symbol and then remove a user from those groups;

Get-ADGroup -Filter 'name -like "@*"' | Remove-ADGroup -identity [USERID]

Get-ADGroup works great, it successfully grabs all of the groups beginning with @, however I get the following error for each and every @ group when piped through to Remove-ADGroup;

Remove-ADGroup : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:1 char:41
+ Get-ADGroup -Filter 'name -like "@*"' | Remove-ADGroup -identity [USERID]
+                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidArgument: CN=@Workplace,O...ife,DC=co,DC=uk:PSObject) [Remove-ADGroup], ParameterBindingException
+ FullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.RemoveADGroup

I can't figure out why the pipe won't work.

Upvotes: 1

Views: 1456

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174660

Remove-ADGroup will remove the group entirely - this is definitely not what you want.

Use Remove-ADGroupMember instead:

Get-ADGroup -Filter 'name -like "@*"' | Remove-ADGroupMember -Members [USERID]

Upvotes: 2

Related Questions