Reputation: 3
I have created a short snippet that should remove members in a list from a specific AD group.
Remove-QADGroupMember -Identity "usergroup" -Member (Get-Content "C:\Users\blabla\users.txt")
The problem is that the list contains alot of old non-existing users. As soon as the script hits a invalid user it stops. Is there any way to have PS ignore users it cannot find without making this too complex?
Upvotes: 0
Views: 16876
Reputation: 21
This is how I did it, because I needed to remove all the users for a bunch of groups rather than a user list.
Finds me the groups that were setup for a project
$OldGroups = Get-ADGroup -filter {Name -like "<*ProjectName>"}
Clears the Members
property of the group effectively removing all the users
$OldGroups | foreach {Set-ADGroup $_ -Clear Member}
Upvotes: 2
Reputation: 66
You can use try/catch in Powershell. It looks like:
Get-Content "C:\Users\blabla\users.txt" | foreach {
Try {
Remove-QADGroupMember -Identity "usergroup" -Member $_
}
Catch [System.Exception] {
"$_ isn't exit"
}
}
I didn't run the code. You can do some tuning on it.
http://blogs.technet.com/b/heyscriptingguy/archive/2010/03/11/hey-scripting-guy-march-11-2010.aspx is a good post on this topic.
Upvotes: 1