MilesLogan
asked on
removing Users from an AD group - set a limit on how many can be removed
Hi EE
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.t xt){
GC C:\PS\groups.txt | % {
$Group = $_
#using try catch to capture the errors
Try{
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"
}
}Catch{
New-Object PSObject -Property @{
GroupName = $Group
User = $User
Status = "Failed"
}
}
}
}) | export-csv "C:\PS\RemovedFromgroup_$( (Get-Date -Format MM-dd-yyyy-hhmmss)).csv" -NoTypeInformation
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.t
GC C:\PS\groups.txt | % {
$Group = $_
#using try catch to capture the errors
Try{
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"
}
}Catch{
New-Object PSObject -Property @{
GroupName = $Group
User = $User
Status = "Failed"
}
}
}
}) | export-csv "C:\PS\RemovedFromgroup_$(
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Forgot to close this ..thank you !
ASKER