Enable External Sharing In SharePoint Online Using Powershell

The below article explains how to enable external sharing using SharePoint Admin Center and using PowerShell.
 
Basically, some project users want to share some of the project documents outside of the organization users like vendors, clients, or partners. So SharePoint provides an extensive solution to share the documents with them.
 
For your SharePoint Online Tenant and for an individual site collection, you can choose from the following options.
  • Don’t allow sharing outside your organization.
  • Allow sharing only with the external users that already exist in your organization’s directory.
  • Allow external users who accept sharing invitations and sign in as authenticated users.
  • Allow sharing with all external users, and by using anonymous access links.
Before that, we should understand what policy the organization has set up, by navigating to SharePoint Online Admin Center >> Sharing option. Here, you can understand your organization's sharing policy. In my scenario, I am using the default setting on my tenant. 
 
SharePoint  

Initially, when the end-user is trying to share some document, folder, or site to the external users, where the external access is not enabled, they will get the error message like below.

SharePoint  

When the user reports this kind of problem, we directly go to SharePoint Admin Center and select the site collection which the user is trying to share to external users, and click the "Sharing" option on the ribbon, like highlighted below.

SharePoint  

Then, you will see some options like in the below image and as per your organization policy, you can select one of them and click "OK".

SharePoint  

In my scenario, I am selecting the third option “Allow external users who accept sharing invitations and sign in as authenticated users” and the site owners only can invite the external users for better security purpose.

SharePoint
 
Then, it will take some time to apply the changes to the selected site collection.

SharePoint
 
So, once the external sharing is enabled to your site collection, go to the SharePoint page and use the "Share" option. You can see that this time, the external users are recognized by SharePoint.
 
SharePoint

Once you click the "Share" button, the external user will get an e-mail notification, like” <username> wants to share you <Document Name>”

And, we can do this operation via PowerShell also. Find the PowerShell script below.

  1. // Connect the SharePoint Admin center  
  2. Connect-SPOService -Url https://tenant-admin.sharepoint.com  
  3.   
  4. // Get the site collect URL  
  5. $siteCollectionURL = “https://tenant.sharepoint.com/sitepages/home.aspx”  
  6.   
  7. // Set the Sharing facility disable  
  8. Set-SPOSite -Identity $siteCollectionURL -SharingCapability Disabled  
  9.   
  10. // Set the Sharing facility only authenticated users  
  11. Set-SPOSite -Identity $siteCollectionURL -SharingCapability ExternalUserSharingOnly  
  12.   
  13. // (Optional) – this will set only site owners can invite the external users  
  14. Set-SPOSite -Identity $siteCollectionURL -DisableSharingForNonOwners  
  15.   
  16. // Set sharing facility as anonymous access  
  17. Set-SPOSite -Identity $siteCollectionURL -SharingCapability ExternalUserAndGuestSharing