SharePoint Online: Set “Who Can View Group Membership” to Everyone using PowerShell

Requirement: Set Who Can View Group Membership to Everyone.

How to Set Who Can View Group Membership to Everyone in SharePoint Online?

By default, in SharePoint, Everyone can view the membership of that group. However, there could be some scenarios where you want to set the group settings to allow only group members to see the other users who have been granted permission to manage the group. On the other hand, You may want to open the group membership to be available for everyone. So, depending on the specific permissions requirements, you can set the group settings.

Follow these steps to change who can view group membership in SharePoint Online:

  1. Go to Site Settings >>  People and Groups
  2. Pick the SharePoint Online group you want to change the group membership view settings
  3. From the group page, Click on Settings >> Group Settings 
  4. Under “Group Settings”, Set “Who can view the membership of the group?” to “Everyone”. Hit OK to save your changes.

PowerShell to Set the View Group Membership to Everyone:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Config Variables
$SiteURL="https://Crescent.sharepoint.com/"
$GroupName="Team Site Members"

Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials

    #Get the Group
    $Group = $Ctx.web.SiteGroups.GetByName($GroupName)
    $Ctx.Load($Group)
    $Ctx.ExecuteQuery()

    #Set Group settings: Who can view the membership of the group? Everyone 
    $Group.OnlyAllowMembersViewMembership = $False
    $Group.Update()
    $Ctx.ExecuteQuery()

    Write-host "Group Settings Updated" -ForegroundColor Green  
}
Catch {
    write-host -f Red "Error Updating Group Settings!" $_.Exception.Message
}

PowerShell to Set View Group Membership to Everyone for All Groups in SharePoint Online:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Config Variables
$SiteURL="https://Crescent.sharepoint.com/sites/marketing"

Try {
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
    
    #Get all groups of the site
    $Groups = $Ctx.Web.SiteGroups
    $Ctx.load($Groups)
    $Ctx.ExecuteQuery()
    
    #Iterate through each Group - Exclude SharePoint Online System Groups!
    ForEach($Group in $Groups| Where {$_.OwnerTitle -ne "System Account"})
    {
        #Set Group settings: Who can view the membership of the group? Everyone 
        $Group.OnlyAllowMembersViewMembership = $False
        $Group.Update()
        $Ctx.ExecuteQuery()

        Write-host "Group Settings Updated for $($Group.Title)" -ForegroundColor Green  
    }
}
Catch {
    write-host -f Red "Error Updating Group Settings!" $_.Exception.Message
}

PnP PowerShell to Change Group Settings

#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/Retail"
$GroupName = "Retail Members"

#Connec tot SharePoint Online
Connect-PnPOnline -URL $SiteURL -Interactive

#Change Group Settings
Set-PnPGroup -Identity $GroupName -OnlyAllowMembersViewMembership:$false

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

Leave a Reply

Your email address will not be published. Required fields are marked *