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

Usage of the Script

Script use cases

The code is presented for four different cases,

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. $sites = Get-SPSite -Limit All
  6. $searchKey = ".rdl"
  7. $results = @()
  8. foreach ($site in $sites)
  9. {
  10. foreach ($web in $site.AllWebs)
  11. {
  12. Write-Host Searching within $web.Url
  13. foreach ($list in $web.Lists)
  14. {
  15. if($list.BaseType -eq "DocumentLibrary")
  16. {
  17. foreach($item in $list.Items)
  18. {
  19. if($item.Name.Contains($searchKey))
  20. {
  21. $RowDetails = @{
  22. “File Location” = $web.Url + "/" + $item.Url
  23. }
  24. $results += New-Object PSObject –Property $RowDetails
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }
  31. $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. $webApp = "http://sharepoint.dreambox.com"
  6. $sites = Get-SPSite -WebApplication $webApp -Limit All
  7. $searchKey = "Self Improvement"
  8. $results = @()
  9. foreach ($site in $sites)
  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 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. $site = Get-SPSite “http://sharepoint.dreambox.com/sites/site1”
  6. $searchKey = ".docx"
  7. $results = @()
  8. foreach ($web in $site.AllWebs)
  9. {
  10. Write-Host Searching within $web.Url
  11. foreach ($list in $web.Lists)
  12. {
  13. if($list.BaseType -eq "DocumentLibrary")
  14. {
  15. foreach($item in $list.Items)
  16. {
  17. if($item.Name.Contains($searchKey))
  18. {
  19. $RowDetails = @{
  20. “File Location” = $web.Url + "/" + $item.Url
  21. }
  22. $results += New-Object PSObject –Property $RowDetails
  23. }
  24. }
  25. }
  26. }
  27. }
  28. $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. $web = Get-SPWeb “http://sharepoint.dreambox.com/sites/site1/web1”
  6. $searchKey = ".pdf"
  7. $results = @()
  8. foreach ($list in $web.Lists)
  9. {
  10. if($list.BaseType -eq "DocumentLibrary")
  11. {
  12. foreach($item in $list.Items)
  13. {
  14. if($item.Name.Contains($searchKey))
  15. {
  16. $RowDetails = @{
  17. “File Location” = $web.Url + "/" + $item.Url
  18. }
  19. $results += New-Object PSObject –Property $RowDetails
  20. }
  21. }
  22. }
  23. }
  24. $results | Export-CSV –Path C:\MySearchedFiles.csv -NoTypeInformation