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

Posted on 2014-08-19
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
    LVL 38

    Accepted Solution

    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

    LVL 2

    Author Comment

    that was it footech !! thanks so much
    LVL 2

    Author Closing Comment

    Forgot to close this ..thank you !

    Featured Post

    PRTG Network Monitor: Intuitive Network Monitoring

    Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

    Join & Write a Comment

    Are you one of those front-line IT Service Desk staff fielding calls, replying to emails, all-the-while working to resolve end-user technological nightmares? I am! That's why I have put together this brief overview of tools and techniques I use in o…
    In this previous article (, we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
    This tutorial will walk an individual through the steps necessary to enable the VMware\Hyper-V licensed feature of Backup Exec 2012. In addition, how to add a VMware server and configure a backup job. The first step is to acquire the necessary licen…
    This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now