0

I had a request from some co-workers to build them a PowerShell to search sites for instances of text that is not accessible so they can replace it. Specifically NA or N/A.

I have a PowerShell working after doing some research. The issue is that the sites are from the entire SharePoint tenant instead of just the site and its subsites to keep things at a smaller level and easier to manage and tackle the script is below. How can I restrict the search to just be the site and its subsites.

I've removed the full URL for reasons. I tried to first restrict it to just the site pages library and then further using the SiteId but results were still the same.

$url="tentant/sites/siteUrl"  
$keyword='N/A OR NA'  
$currentTime=$(get-date).ToString("yyyyMMddHHmmss");  
$outputFilePath="C:\results-"+$currentTime+".csv"  
#$credentials=Get-Credential  
 
## Connect to SharePoint Online site  
Connect-PnPOnline -Url $url -Interactive 
 
## Executes an arbitrary search query against the SharePoint search index  
$results=Submit-PnPSearchQuery -Query $keyword -Refiners "contentclass:0x01010007F4C7534B47E89A77154B10AD7C7C4372 SiteId:{redacted}
 
## Get the results in the hash table  
$hashTable=@()  
foreach($resultRow in $results.ResultRows)  
{  
    $obj=New-Object PSObject  
    $resultRow.GetEnumerator()| ForEach-Object{ $obj | Add-Member Noteproperty $_.Key $_.Value}  
    $hashTable+=$obj;  
    $obj=$null;  
}  
 
## Export to CSV  
$hashtable | export-csv $outputFilePath -NoTypeInformation  
 
## Disconnect the context  
Disconnect-PnPOnline  

1 Answer 1

2

You can use try using Path property in your search query to limit search results to specific SharePoint site.

For example:

$results=Submit-PnPSearchQuery -Query "N/A OR NA Path:https://myCompany.sharepoint.com/teams/humanresources"

Reference: How to recognise the differance between a "News Post" and a "Page" programmatically with PowerShell?

Documentation: Submit-PnPSearchQuery

7
  • Awesome I will definitely give this a try. While it works if I dont restrict it somehow it just runs forever as we have a ton of sites and I want to make it as easy as possible to just go site by site.
    – OMGDrAcula
    Commented Mar 29 at 12:22
  • 1
    Sure, let me know if it works for you. Commented Mar 29 at 12:24
  • 1
    Check this docs for query building: Building search queries in SharePoint and Keyword Query Language (KQL) syntax reference. Commented Mar 29 at 12:42
  • 1
    Awesome I will take a look thank you soo much!
    – OMGDrAcula
    Commented Mar 29 at 12:45
  • 1
    I updated my script to do "NA" after reading some documentation which I believe should treat NA as a word. Than you again so much for your help!
    – OMGDrAcula
    Commented Mar 29 at 13:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.