[Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2014-08-19
Medium Priority
Last Modified: 2014-08-24

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
Question by:MilesLogan
  • 2
LVL 41

Accepted Solution

footech earned 2000 total points
ID: 40271516
Try changing your first line to something like below.  It will only read the first 100 lines of the Users.txt file.
@(Foreach ( $User in (GC \\Servername\Share\Users.txt -TotalCount 100) ){

Open in new window


Author Comment

ID: 40271657
that was it footech !! thanks so much

Author Closing Comment

ID: 40282379
Forgot to close this ..thank you !

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A walk-through example of how to obtain and apply new DID phone numbers to your cloud PBX enabled users that are configured in Office 365. Whether you have 1, 10 or 100+ users in your tenant, it's quite easy to get them phone-enabled and making/rece…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question