Update Sharepoint external sharing settings on all sites with powershell

Safak 20 Reputation points
2024-01-04T14:25:35.6933333+00:00

Hi,

I would like to change the sharing settings on all sites which doesn't match our desired setting.

# Customize these variables for your needs
$sharepointAdminURL = "https://abc-admin.sharepoint.com"
$currentSharingSetting = "ExistingExternalUserSharingOnly"
$desiredSharingSetting = "ExternalUserSharingOnly"
# Check if needed modules are installed
if ($module = Get-Module -Name Microsoft.Online.SharePoint.PowerShell -ListAvailable | Select Name,Version) {
    #write-host "installed"
    } else {
        write-host "SharePoint module Not installed, install with Install-Module -Name Microsoft.Online.SharePoint.PowerShell" -ForegroundColor Red
        exit
        }
# Connect to SharePoint Online if needed
try { 
    $testConnection = Get-SPOGeoStorageQuota # Quick connection test
} 
catch {
    Write-Host "Not connected. Please authenticate in the pop-up window"; Connect-SPOService -Url $sharepointAdminURL
}
# Get sites that match our current sharing setting and write them to the screen
GET-SPOSITE -LIMIT ALL | WHERE-OBJECT {$_.SharingCapability -eq $currentSharingSetting} | FORMAT-TABLE Title,URL,SharingCapability
# If the output looks good, uncomment the following line and run the script again to make the change!
GET-SPOSITE -LIMIT ALL | WHERE-OBJECT {$_.SharingCapability -eq $currentSharingSetting} | FORMAT-TABLE Title,URL,SharingCapability | SET-SPOSITE -SharingCapability $desiredSharingSetting

All the sites are displayed correctly but when I wan't so set the correct setting I'm getting following error message:

SET-SPOSITE : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters 
that take pipeline input.
At line:22 char:134
+ ... ingCapability | SET-SPOSITE -SharingCapability $desiredSharingSetting
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (Microsoft.Power...t.FormatEndData:PSObject) [Set-SPOSite], ParameterBindingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.Online.SharePoint.PowerShell.SetSite

Can someone help me with this?

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
{count} votes

Accepted answer
  1. Yanli Jiang - MSFT 31,596 Reputation points Microsoft External Staff
    2024-01-05T06:53:02.0266667+00:00

    Hi @Safak ,

    The error message you are receiving indicates that the input object cannot be bound to any parameters for the command. This may be because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.

    To fix this issue, you can try removing the FORMAT-TABLE cmdlet from the second GET-SPOSITE command. This will ensure that the output is in the correct format for the SET-SPOSITE command to accept it as input.

    Here is the updated code:

    # Customize these variables for your needs
    $sharepointAdminURL = "https://abc-admin.sharepoint.com"
    $currentSharingSetting = "ExistingExternalUserSharingOnly"
    $desiredSharingSetting = "ExternalUserSharingOnly"
    # Check if needed modules are installed
    if ($module = Get-Module -Name Microsoft.Online.SharePoint.PowerShell -ListAvailable | Select Name,Version) {
        #write-host "installed"
        } else {
            write-host "SharePoint module Not installed, install with Install-Module -Name Microsoft.Online.SharePoint.PowerShell" -ForegroundColor Red
            exit
            }
    # Connect to SharePoint Online if needed
    try { 
        $testConnection = Get-SPOGeoStorageQuota # Quick connection test
    } 
    catch {
        Write-Host "Not connected. Please authenticate in the pop-up window"; Connect-SPOService -Url $sharepointAdminURL
    }
    # Get sites that match our current sharing setting and write them to the screen
    GET-SPOSITE -LIMIT ALL | WHERE-OBJECT {$_.SharingCapability -eq $currentSharingSetting} | FORMAT-TABLE Title,URL,SharingCapability
    # If the output looks good, uncomment the following line and run the script again to make the change!
    GET-SPOSITE -LIMIT ALL | WHERE-OBJECT {$_.SharingCapability -eq $currentSharingSetting} | SET-SPOSITE -SharingCapability $desiredSharingSetting
    

    For your reference:

    Set-SPOSite


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.