Skip to content

Instantly share code, notes, and snippets.

@joerodgers
Last active November 26, 2025 13:25
Show Gist options
  • Select an option

  • Save joerodgers/60cd08247e46e1a2774e7bb2ce63eed0 to your computer and use it in GitHub Desktop.

Select an option

Save joerodgers/60cd08247e46e1a2774e7bb2ce63eed0 to your computer and use it in GitHub Desktop.
#requires -modules "PnP.PowerShell"
# requires SharePoint > application > sites.fullcontrol.all permissions
$sites = "https://m365cpi68930152.sharepoint.com/sites/security-policies1",
"https://m365cpi68930152.sharepoint.com/sites/security-policies2"
$timestamp = Get-Date -Format FileDateTime
foreach( $site in $sites )
{
Write-Host "[$(Get-Date)] - Testing Site: $site"
Connect-PnPOnline -Url $site `
-ClientId $env:CDX_CLIENTID `
-Thumbprint $env:CDX_THUMBPRINT `
-Tenant $env:CDX_TENANTID
$context = Get-PNPContext
$hostname = [System.Uri]::new( $context.Url ).Host
$lists = Get-PnpList -Includes RootFolder.ServerRelativeUrl | Where-Object { $_.Hidden -eq $false -and $_.BaseType -eq "DocumentLibrary" }
$searchResults = foreach( $list in $lists )
{
if( $list.ItemCount -gt 0 )
{
$path = "`"https://$hostname$($list.RootFolder.ServerRelativeUrl)`""
$results = Invoke-PnPSearchQuery -Query "path:$path" -TrimDuplicates $true
if( $results.TotalRows -gt 0 )
{
# one or more items in doc lib are indexed
[PSCustomObject] @{
Site = $context.Url
List = $list.Title
ListItemCount = $list.ItemCount
SearchResultsCount = $results.TotalRows
}
}
}
}
if( $searchResults )
{
$searchResults | Export-Csv -Path "C:\_temp\SeachResults_$timestamp.csv" -Append -NoTypeInformation
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment