Question

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?

Was it helpful?

Solution 2

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.

OTHER TIPS

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}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top