How to find out a user who is part of the SPGroups in a site collection using PowerShell.I want to pass the userid and get the names of groups where he is part of.Later I want to delete his id from all these SPGroups.I wrote the below code, but am not getting success.

 $sitee= get-spsite "https://myDMS.mycompany.com"
 $webb= $sitee.Rootweb
 $inputusername ="in\userid"
 $Allgroups = $webb.SiteGroups
 foreach(singeGrp in $Allgroups)
   SPUser muser = new-object SPUser
   foreach(SPuser suser in singeGrp.Users)                  
           if(suser.Loginname -eq  muser.Loginname) 

You create the object muser, but never initialize it. So, your comparison is always false.

I would suggest inspecting the Groups property of the SPUser object. (The following is not tested since I don't have an environment handy.)

$site = get-spite "{siteUrl}"
$web = $site.RootWeb
$user = $web.AllUsers["{domain\\user_alias}"]
Foreach($group in $user.Groups) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.