Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell Remove all users from a specific group

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...

like image 459
KathyBlue Avatar asked Oct 10 '14 16:10

KathyBlue


2 Answers

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}
like image 63
Dane Boulton Avatar answered Nov 04 '22 10:11

Dane Boulton


Here is an alternative way:

Remove-ADGroupMember "test_group" -Members (Get-ADGroupMember "test_group") -Confirm:$false
like image 25
OrcoXP Avatar answered Nov 04 '22 11:11

OrcoXP