7

I'm trying to clean all users from Local Group test_group by executing the following command below on Windows 2008 R2 Standard, PowerShell 2.0.

Get-ADGroupMember "test_group" | ForEach-Object {Remove-ADGroupMember "test_group" $_ -Confirm:$false} 

It throws the following error, most probably because I'm using v2.0?:

The term 'Get-ADGroupMember' is not recognized as the name of a cmdlet, function, script file, or operable program. Che ck the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:18 + Get-ADGroupMember <<<< "test_group" | ForEach-Object {Remove-ADGroupMember "test_group" $_ -Confirm:$false} + CategoryInfo : ObjectNotFound: (Get-ADGroupMember:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException

I tried many ideas from this article and its comments, and I couldn't get any to work but I'm not a sysadmin and I'm not sure if I'm not missing something?: http://blogs.technet.com/b/heyscriptingguy/archive/2009/07/28/hey-scripting-guy-how-do-i-remove-all-group-members-in-active-directory.aspx

Please help, I have around 300 groups to clean on Monday and I don't want to do it manually...

4 Answers 4

10

not sure if you if this is a typo or this was how you were running the command but it should be get-adgroupmember

Get-ADGroupMember "test_group" | ForEach-Object {Remove-ADGroupMember "test_group" $_ -Confirm:$false} 

That worked for me had to refresh the ADUC ou to see the change though

EDIT

import the ActiveDirectory module first then try and run the command.

import-module activedirectory Get-ADGroupMember "test_group" | ForEach-Object {Remove-ADGroupMember "test_group" $_ -Confirm:$false} 
Sign up to request clarification or add additional context in comments.

8 Comments

Right, now it throws the following error: The term 'Get-ADGroupMember' is not recognized as the name of a cmdlet, function, script file, or operable program. Che ck the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:18 + Get-ADGroupMember <<<< "test_group" | ForEach-Object {Remove-ADGroupMember "test_group" $_ -Confirm:$false} + CategoryInfo : ObjectNotFound: (Get-ADGroupMember:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
I think it's because I'm using PowerShell 2.0 but I cannot upgrade. Is there a workaround?
did you load the active directory module? import-module activedirectory? im pretty sure it is in powershell 2 but I dont have it to test.
How do I do that? Can you explain the steps if possible? Thanks for the patience!
see my edit above. import the module then run the command as long as you have the AD tools installed it should load the module.
|
6

Here is an alternative way:

Remove-ADGroupMember "test_group" -Members (Get-ADGroupMember "test_group") -Confirm:$false 

2 Comments

-Identity is missing after Get-ADGroupMember
This will give an error if there are no members to remove
6

Building on Zhenia Shapiro's answer:

Get-ADGroup "test_group" | Set-ADGroup -Clear member 

Comments

0
set-adgroup -clear 

the tricky part for me was to remember use ldap attr. name, "member" and not "members" that intuitively f(ol)lows from get-adgroup -prop members

To modify an object property, you must use the LDAP display name: Documentation

1 Comment

Sorry, this answer is not very helpful. When I type you command line I got an error "missing argument". Please discribe how your solution solves the problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.