简体   繁体   中英

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

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}

这是另一种方法:

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

基于珍尼亚夏皮罗的回答:

Get-ADGroup "test_group" | Set-ADGroup -Clear member
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM