Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

Professional Opinions
Ask a Question
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

troubleshooting Question

removing Users from an AD group - set a limit on how many can be removed

Avatar of MilesLogan
MilesLoganFlag for United States of America asked on
Active DirectoryPowershellWindows Server 2008
3 Comments1 Solution476 ViewsLast Modified:

Subsun helped me with this script yesterday on adding users to a group . I modified it to remove users from a group but
wanted to see if there was a way to limit the amount of users the script can remove  ?

So the users.txt file will be filled in by an application owner .. his group containts 1000's of users .. I dont want for him to
accidentally put 1000's of users to remove by mistake .. I want for the script to only remove lets say 100 users even if the
Users.txt file has 10000 SamAccountnames listed .

Any ideas ?

$(Foreach ($User in GC \\Servername\Share\Users.txt){
GC C:\PS\groups.txt | % {
   $Group = $_
   #using try catch to capture the errors
        Remove-ADGroupmember -identity $Group -member $User -ea stop
      #create custom PS object to output the command status..
        New-Object PSObject -Property @{
      GroupName = $Group
      User = $User
      Status = "Success"
      New-Object PSObject -Property @{
      GroupName = $Group
      User = $User
      Status = "Failed"
}) | export-csv "C:\PS\RemovedFromgroup_$((Get-Date -Format MM-dd-yyyy-hhmmss)).csv" -NoTypeInformation
Avatar of footech
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answers