SharePoint Custom Search Script

This article will explain the PowerShell script searches for files within Document libraries in SharePoint (farm level, site collection level, web level etc.) based on the search parameters you provide. It can also be used for searching in SharePoint for any files with the specified extension. The results are returned in a .CSV format.

This article will explain the PowerShell script that searches for files within document libraries in SharePoint (farm level, site collection level, web level etc.) based on the search parameters you provide. It can also be used for searching in SharePoint for any files with specified extension. The results are returned in .CSV format. 

Examples of scenarios

  • Search the entire farm for files that have names like “Training Copy”.
  • Search the site collection http://sharepoint.mydreambox.com/sites/site1 for all PDF files, RDL files, etc.
  • Search the web http://sharepoint.mydreambox.com/sites/site1/web1 for the file named “AXL8rt.docx”, etc.

Usage of the Script

  • Replace the $webApp, $sites, etc. with appropriate values
  • Replace the $searchKey with the desired search queries or file extension that is to be searched for
  • Make sure the account that is running these scripts have necessary privileges in SharePoint

Script use cases

The code is presented for four different cases,

  • Searching for an entire farm
  • Searching within a web application
  • Searching within a site collection
  • Searching within a web

Output

The output will be in the form of a CSV file, as displayed below. It will contain all the file location URLs.

Output

Case I - For searching within the entire farm

  1. if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)   
  2. {  
  3.     Add-PSSnapin "Microsoft.SharePoint.PowerShell"  
  4. }  
  5.   
  6. $sites =  Get-SPSite  -Limit All  
  7. $searchKey = ".rdl"  
  8. $results = @()  
  9.   
  10. foreach ($site in $sites)  
  11. {  
  12.     foreach ($web in $site.AllWebs)  
  13.     {  
  14.       Write-Host Searching within $web.Url   
  15.       foreach ($list in $web.Lists)  
  16.        {  
  17.         if($list.BaseType -eq "DocumentLibrary")   
  18.          {  
  19.              foreach($item in $list.Items)  
  20.               {  
  21.                 if($item.Name.Contains($searchKey))  
  22.                 {  
  23.                     $RowDetails = @{  
  24.             “File Location” = $web.Url + "/" + $item.Url  
  25.         }  
  26.         $results += New-Object PSObject –Property $RowDetails  
  27.                 }  
  28.               }               
  29.           }  
  30.        }  
  31.     }  
  32.  }  
  33. $results | Export-CSV –Path C:\MySearchedFiles.csv -NoTypeInformation  

Case II - For searching within a Web Application

  1. if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)   
  2. {  
  3.     Add-PSSnapin "Microsoft.SharePoint.PowerShell"  
  4. }  
  5.   
  6. $webApp = "http://sharepoint.dreambox.com"  
  7. $sites =  Get-SPSite -WebApplication $webApp  -Limit All  
  8. $searchKey = "Self Improvement"  
  9. $results = @()  
  10.   
  11. foreach ($site in $sites)  
  12. {  
  13.     foreach ($web in $site.AllWebs)  
  14.     {  
  15.       Write-Host Searching within $web.Url   
  16.       foreach ($list in $web.Lists)  
  17.        {  
  18.         if($list.BaseType -eq "DocumentLibrary")   
  19.          {  
  20.              foreach($item in $list.Items)  
  21.               {  
  22.                 if($item.Name.Contains($searchKey))  
  23.                 {  
  24. $RowDetails = @{  
  25.             “File Location” = $web.Url + "/" + $item.Url  
  26.         }  
  27.         $results += New-Object PSObject –Property $RowDetails  
  28.   
  29.                 }  
  30.               }               
  31.           }  
  32.        }  
  33.     }  
  34.  }  
  35. $results | Export-CSV –Path C:\MySearchedFiles.csv -NoTypeInformation  

Case III - For searching within a Site Collection

  1. if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)   
  2. {  
  3.     Add-PSSnapin "Microsoft.SharePoint.PowerShell"  
  4. }  
  5.   
  6. $site =  Get-SPSite “http://sharepoint.dreambox.com/sites/site1”  
  7. $searchKey = ".docx"  
  8. $results = @()  
  9.   
  10.   
  11.     foreach ($web in $site.AllWebs)  
  12.     {  
  13.       Write-Host Searching within $web.Url   
  14.       foreach ($list in $web.Lists)  
  15.        {  
  16.         if($list.BaseType -eq "DocumentLibrary")   
  17.          {  
  18.              foreach($item in $list.Items)  
  19.               {  
  20.                 if($item.Name.Contains($searchKey))  
  21.                 {  
  22.                     $RowDetails = @{  
  23.             “File Location” = $web.Url + "/" + $item.Url  
  24.         }  
  25.         $results += New-Object PSObject –Property $RowDetails  
  26.   
  27.                 }  
  28.               }               
  29.           }  
  30.        }  
  31.     }  
  32.  $results | Export-CSV –Path C:\MySearchedFiles.csv -NoTypeInformation  

Case IV - For searching within a Web

  1. if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)   
  2. {  
  3.     Add-PSSnapin "Microsoft.SharePoint.PowerShell"  
  4. }  
  5.   
  6. $web =  Get-SPWeb “http://sharepoint.dreambox.com/sites/site1/web1”  
  7. $searchKey = ".pdf"  
  8. $results = @()  
  9.   
  10.       foreach ($list in $web.Lists)  
  11.        {  
  12.         if($list.BaseType -eq "DocumentLibrary")   
  13.          {  
  14.              foreach($item in $list.Items)  
  15.               {  
  16.                 if($item.Name.Contains($searchKey))  
  17.                 {  
  18.         $RowDetails = @{  
  19.             “File Location” = $web.Url + "/" + $item.Url  
  20.         }  
  21.         $results += New-Object PSObject –Property $RowDetails  
  22.                 }  
  23.               }               
  24.           }  
  25.        }  
  26.   
  27. $results | Export-CSV –Path C:\MySearchedFiles.csv -NoTypeInformation