none
How can I check if a user already exist in a sharepoint group or not in powershell ? RRS feed

  • Question

  • How can I check if a user already exist in a sharepoint group or not ?

    I have tried following code :

     if (!($user.MemberOf -match $group)){
             Write-Output "User is already a member of $group"      
            }
            else
            {
            new-spuser -UserAlias $User -Web $web -Group $group -ErrorAction Stop
             Write-Output "User is not a member of $group"
            }

    But its not working properly, Any help on this please ? Thanks in advance !

    • Edited by Mind2013 Tuesday, September 24, 2013 9:48 AM
    Tuesday, September 24, 2013 9:47 AM

Answers

  • A quick way to check is to use the SPGroup.ContainsCurrentUser property. This will return true if the current user is a direct member, or indirect (the user is a member of an Active Directory group that is a member of the SharePoint group).

    E.g.

    $w = Get-SPWeb "http://devmy101"
    $g = $w.Groups["Home Visitors"]
    $g.ContainsCurrentUser
    ## Returns true or false.


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    I just added a webpart to the TechNet Gallery that allows administrative users to upload, crop and format user profile photos. Check it out here: Upload and Crop User Profile Photos


    • Edited by Matthew Yarlett Tuesday, September 24, 2013 10:05 AM
    • Proposed as answer by Sairams Tuesday, September 24, 2013 10:08 AM
    • Marked as answer by Mind2013 Wednesday, September 25, 2013 5:21 AM
    Tuesday, September 24, 2013 10:04 AM
  • Depending on how you add people to the group, you might need to take different approaches. 

    Different results get returned depending on whether the user is a direct member of the group, or an indirect member of the group (because they belong to an Active Directory group that is a member of the SharePoint group).

    Example: User1, who is a direct member of the group

    $group.Users.GetByEmail($user1.Email) #returns an SPUser object
    $group.Users.GetByID($user1.ID) #returns an SPUser object
    $group.Users[$user1] #returns an SPUser object
    Example: User2, who is an  in-direct member of the group (not a member of the SPGroup, but is a member of a domain group, which in turn is a member of the SPGroup)

    $group.Users.GetByEmail($user2.Email) #Throws an exception: "User cannot be found." $group.Users.GetByID($user2.ID) #Throws an exception: "User cannot be found." $group.Users[$user2] #Returns null

    If all you want to do is add the user to the (SharePoint) group, you can call SPGroup.AddUser(SPUser), which will add the user to the group. This method works if the user is or isn't already a member of the group. This might be you best choice.

    The other way would be to get all the groups users, check the "IsDomainGroup" property. If it's true, then you would need to enumerate the domain group (and any groups nested in the domain group), so see if the user is a member of one of the domain groups.


    Regards, Matthew <br/> MCPD | MCITP<br/> <a href="http://matthewyarlett.blogspot.co.uk/" alt="Matthew's Blog">My Blog</a> <br/> Please remember to click &quot;Mark As Answer&quot; if a post solves your problem or &quot;Vote As Helpful&quot; if it was useful. <br/> <br/> See my webpart on the TechNet Gallery that allows administrative users to upload, crop and format user profile photos. Check it out here: <a href="http://gallery.technet.microsoft.com/sharepoint/User-Profile-Picture-b8880e18" alt="Upload and Crop User Profile Photos"> Upload and Crop User Profile Photos</a>

    • Marked as answer by Mind2013 Wednesday, September 25, 2013 5:21 AM
    Tuesday, September 24, 2013 2:33 PM
  • Thanks Matthew  !!! :)

    But I need to check for individual user  , not for the current user, do we have any properties for that ?

    • Marked as answer by Mind2013 Wednesday, September 25, 2013 5:21 AM
    Tuesday, September 24, 2013 10:27 AM

All replies

  • Hi,

    Try to intialize the SPSite object and try to get the user using below code

     $site.RootWeb.Groups["Approvers"].Users.GetByEmail()

    If this statement returns a valid object, user is part of group. If its null user is not part of the group.


    Ram Prasad Meenavalli | MCITP | MCTS SharePoint 2010 | MCPD SharePoint 2010 | http://www.spdeveloper.co.in

    Tuesday, September 24, 2013 9:56 AM
  • A quick way to check is to use the SPGroup.ContainsCurrentUser property. This will return true if the current user is a direct member, or indirect (the user is a member of an Active Directory group that is a member of the SharePoint group).

    E.g.

    $w = Get-SPWeb "http://devmy101"
    $g = $w.Groups["Home Visitors"]
    $g.ContainsCurrentUser
    ## Returns true or false.


    Regards, Matthew
    MCPD | MCITP
    My Blog
    Please remember to click "Mark As Answer" if a post solves your problem or "Vote As Helpful" if it was useful.

    I just added a webpart to the TechNet Gallery that allows administrative users to upload, crop and format user profile photos. Check it out here: Upload and Crop User Profile Photos


    • Edited by Matthew Yarlett Tuesday, September 24, 2013 10:05 AM
    • Proposed as answer by Sairams Tuesday, September 24, 2013 10:08 AM
    • Marked as answer by Mind2013 Wednesday, September 25, 2013 5:21 AM
    Tuesday, September 24, 2013 10:04 AM
  • Thanks Matthew  !!! :)

    But I need to check for individual user  , not for the current user, do we have any properties for that ?

    • Marked as answer by Mind2013 Wednesday, September 25, 2013 5:21 AM
    Tuesday, September 24, 2013 10:27 AM
  • Depending on how you add people to the group, you might need to take different approaches. 

    Different results get returned depending on whether the user is a direct member of the group, or an indirect member of the group (because they belong to an Active Directory group that is a member of the SharePoint group).

    Example: User1, who is a direct member of the group

    $group.Users.GetByEmail($user1.Email) #returns an SPUser object
    $group.Users.GetByID($user1.ID) #returns an SPUser object
    $group.Users[$user1] #returns an SPUser object
    Example: User2, who is an  in-direct member of the group (not a member of the SPGroup, but is a member of a domain group, which in turn is a member of the SPGroup)

    $group.Users.GetByEmail($user2.Email) #Throws an exception: "User cannot be found." $group.Users.GetByID($user2.ID) #Throws an exception: "User cannot be found." $group.Users[$user2] #Returns null

    If all you want to do is add the user to the (SharePoint) group, you can call SPGroup.AddUser(SPUser), which will add the user to the group. This method works if the user is or isn't already a member of the group. This might be you best choice.

    The other way would be to get all the groups users, check the "IsDomainGroup" property. If it's true, then you would need to enumerate the domain group (and any groups nested in the domain group), so see if the user is a member of one of the domain groups.


    Regards, Matthew <br/> MCPD | MCITP<br/> <a href="http://matthewyarlett.blogspot.co.uk/" alt="Matthew's Blog">My Blog</a> <br/> Please remember to click &quot;Mark As Answer&quot; if a post solves your problem or &quot;Vote As Helpful&quot; if it was useful. <br/> <br/> See my webpart on the TechNet Gallery that allows administrative users to upload, crop and format user profile photos. Check it out here: <a href="http://gallery.technet.microsoft.com/sharepoint/User-Profile-Picture-b8880e18" alt="Upload and Crop User Profile Photos"> Upload and Crop User Profile Photos</a>

    • Marked as answer by Mind2013 Wednesday, September 25, 2013 5:21 AM
    Tuesday, September 24, 2013 2:33 PM
  • Thanks ! :)
    Wednesday, September 25, 2013 5:21 AM