Add domain to all SharePoint sites external sharing policy
Office 365 (SharePoint) Friday, 05 June 2020 by paul

To add an Allowed Domain to all SharePoint sites with an external sharing policy limiting by domain the following PowerShell script can be used.

# Add domain to Allowed Domains - replace DOMAIN2.COM as required

Connect-SPOService -Url https://x-admin.sharepoint.com

# List sharepoint collections
Write-Host "Getting list of site collections..."
$sites = get-sposite -limit All | foreach { Get-SPOSite $_ | select url, SharingDomainRestrictionMode, SharingAllowedDomainList} 

$DomainToCheck = "DOMAIN2.COM"
Write-Host "Checking sharing mode in sites"
foreach ($site in $sites) {
   $sharingmode = $site.SharingDomainRestrictionMode      
   If($sharingmode -eq 'AllowList') {
    $sharingdomainlist = $site.SharingAllowedDomainList
	if($sharingdomainlist -like '*'+$DomainToCheck+'*')
		Write-host $site.url "already contains domain - $sharingdomainlist" -foreground green
		Write-host $site.url "does not contain domain  - $sharingdomainlist" -foreground red
		$newdomainlist = $sharingdomainlist+" $DomainToCheck"
		$newdomainlist = $newdomainlist -replace ",", ' '
		Write-host "Setting to $newdomainlist" -foreground red
		set-sposite -identity $site.url -SharingDomainRestrictionMode AllowList -SharingAllowedDomainList $newdomainlist
   } else {
		Write-host $site.url "not using AllowList" -foreground green

It should apply immediately.

Add Comment
Monday, 08 June 2020 by Unknown
How interesting!!