Using Powershell to remove a user from a list of groups -
trying script remove user list of groups.
not sure part of language here wrong. doesn't return error in ise. i'm admittedly rookie in writing own powershell scripts instead of modifying others. appreciated.
import-module activedirectory $group = @('grouopname1','groupname2','groupname3') $user = "testa" if ($user.memberof -like $group) { foreach ($user in $group ) { remove-adprincipalgroupmembership -identity $user -memberof $group -confirm:$false } }
on thing you'll need learn when using powershell it's important test kind of output , object give you.
$user = "testa" | $user.memberof
that give error, because string doesn't have member property "memberof"
$user = get-aduser testa -properties memberof
that give object containing user "testa", , since memberof property isn't retrieved default, need add in.
$user.memberof
this return distinguishedname of groups user member of, not nested groups... require more logic, can find if search it.
your group array work... if replace on $user.memberof returns common name, can compare dns. however, need this:
$groups = $group | foreach ($g in $group) { get-adgroup $g }
not pretty, there.
the final part of loop can this:
foreach ($u in ($user.memberof)) { if (($groups.distinguishedname) -contains $u) { do-whatever } }
i start test inside loop, using write-host or -whatif verify going done.
Comments
Post a Comment