PowerShell script to output what SharePoint Online sites have privacy set to public and private

50 Views Asked by At

I received a request to check what SharePoint sites have private set to private and public.

This is the PowerShell script that I am using.

Sadly, when I open the .csv file, it says all of the sites are public. But in SharePoint online, when I go to SharePoint Admin Center->Active sites->Site name->Settings some sites have privacy set to private.

How do I edit the below script to output what sites have privacy set to private and public?

# Connect to SharePoint Online
Connect-PnPOnline -Url https://yourtenant.sharepoint.com/sites/yoursite -UseWebLogin

# Get all sites within the SharePoint Online tenant
$sites = Get-PnPTenantSite

# Initialize an array to store site privacy information
$sitePrivacyReport = @()

# Iterate through each site
foreach ($site in $sites) {
$siteUrl = $site.Url
$siteTitle = $site.Title

# Get site properties to check if it's public or private
$siteProperties = Get-PnPTenantSite -Url $siteUrl
$privacyStatus = $siteProperties.Status

# Determine if the site is public or private
if ($privacyStatus -eq "Active") {
    $privacy = "Public"
} else {
    $privacy = "Private"
}

# Add site information to the report array
$siteInfo = New-Object PSObject -Property @{
    SiteTitle = $siteTitle
    SiteUrl = $siteUrl
    Privacy = $privacy
}
$sitePrivacyReport += $siteInfo
}

# Define the output path
$outputPath = "C:\Users\USERNAME\Downloads\SharePoint_Site_Privacy_Report.csv"

# Export the report to a CSV file
$sitePrivacyReport | Export-Csv -Path $outputPath -NoTypeInformation

Write-Host "Report saved to: $outputPath"

# Disconnect from SharePoint Online
Disconnect-PnPOnline
1

There are 1 best solutions below

4
Xyza_MSFT On

Using the following pnp powershell to get All the Public or Private Sites in SharePoint :

$AdminCenterURL = "https://yourtenant-admin.sharepoint.com"
 
#Get Credentials to connect
$Cred = Get-Credential
 
#Connect to Tenant Admin
Connect-PnPOnline -URL $AdminCenterURL -Credential $Cred
Get-PnPMicrosoft365Group -IncludeSiteUrl | Select Displayname,Visibility,siteurl

Output: enter image description here


If the answer is helpful, please click "√" on the left panel of the answer and kindly upvote it.