SharePoint Online: Share Site to "Everyone Except External Users" using PowerShell

Requirement: Add everyone except external users in SharePoint Online.

SharePoint Online: Share with Everyone Except External Users Group
Got a requirement to grant access to everyone in the organization to a SharePoint Online site. To share with everyone except external users,
  • Login to your SharePoint Online site >> Click on "Share" button on the top-right corner.
  • In the Add user popup, Enter "Everyone Except External Users", pick the relevant SharePoint group or permission for this group and click on Share.
    sharepoint online share with everyone except external users
You can also do this by going to: Site Settings >> Click on "Site permissions" under "Users and Permissions" group >> Pick the group where you want to add everyone in your organization >> New >> Add Users >> Enter "Everyone except external users" >> Click Share

In case, you don't get "Everyone except external users" in the Share popup page, you have to make sure if everyone except external users claims is enabled at your tenant: SharePoint Online: How to enable Everyone Except External Users?

PowerShell to Add Everyone Except External Users in SharePoint Online
Let's add everyone except external users to site visitors group using PowerShell
#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"
 
#Set Variables
$SiteURL = "https://crescent.sharepoint.com/sites/Marketing"
  
#Get Credentials to connect
$Cred = Get-Credential
  
Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)
 
    #Get the default visitors groups of the site
    $VisitorsGroup = $ctx.web.AssociatedVisitorGroup
    $Ctx.load($VisitorsGroup)
    $Ctx.ExecuteQuery()

    #Resolve User Name by Display Name
    $Principal = [Microsoft.SharePoint.Client.Utilities.Utility]::ResolvePrincipal($Ctx, $Ctx.Web, "Everyone except external users", "All", "All", $Null, $True)
    $Ctx.ExecuteQuery()
    $User = $Ctx.Web.EnsureUser($Principal.Value.LoginName)
    $Ctx.ExecuteQuery()

    #Add user to the group
    $VisitorsGroup.Users.AddUser($User) | Out-Null
    $Ctx.ExecuteQuery()
}
catch {
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red
}

PnP PowerShell to Add Everyone Except External Users to Site
Let's add everyone except external users to the visitors group of the site with PnP PowerShell.
#Config Parameter
$SiteURL = "https://crescent.sharepoint.com/sites/marketing"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Group to Add - Default Visitors group of the site
$Group = Get-PnPGroup -AssociatedVisitorGroup

#Add Everyone except External Users to Visitors group
Add-PnPUserToGroup -Identity $Group -LoginName "everyone except external users" 

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.