SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I've a requirement to find all the files under all websites present in sharepoint.

I've use .SubFolders , but it works for a single root folder only.

Is there any alternate way for recursive searching?

I tried

Get-ChildItem $src -recurse | where {$_.PSIsContainer}

but it's not accepting URL as source.

share|improve this question

migrated from Apr 6 '13 at 14:20

This question came from our site for professional and enthusiast programmers.

For some guidance on what parameters any powershell command accepts the Get-Help and Get-Command powershell parameters are very useful.


get-help get-childitem -full
get-command Get-ChildItem -syntax

If you run either of these commands you can see Get-ChildItem does not accept a url as input.

Its not clear at what level you wish to iterate from when you ask 'under all websites present in sharepoint'. You may also have a misunderstanding between windows directory folders and a folder in SharePoint, they are not the same thing.

The following script could be written in a shorter form using pipes but in its current form may be a bit easier to follow.

Add-PSSnapin microsoft.sharepoint.powershell

Function GetFiles($folder)
   Write-Host "+"$folder.Name
   foreach($file in $folder.Files)
       Write-Host "`t" $file.Name

   # Use recursion to loop through all subfolders.
   foreach ($subFolder in $folder.SubFolders)
       Write-Host "`t" -NoNewline

$webApps = Get-SPWebApplication  # Get All Web Applications

foreach($webApp in $webApps)  # Iterate around all web applications
    write-host $webApp.Url -ForegroundColor Red

    foreach ($SPsite in $webApp.Sites)  # Iterate around all site collections in a web application
       write-host $SPSite.Url -ForegroundColor DarkRed

       foreach($SPweb in $SPsite.AllWebs)  # Iterate around all sites in a site collection
            write-host $SPweb.title ":" $SPweb.URL -ForegroundColor DarkMagenta

            foreach($list in $SPweb.lists)  # Iterate around all lists in a site
                foreach($item in $list.items)
                    Write-Host $item.Title

share|improve this answer
This script needs a slight modification as it fails on the List "Attachments" folder. While all other folders are "SPFolder" object and have "SubFolders" The List "Attachments" folder is actually a "SPFolderCollection" object. – SP Combatant Jan 9 '15 at 2:32
In the GetFiles function replace... foreach ($subFolder in $folder.SubFolders) ...with something like this... if ($folder.GetType() -eq [Microsoft.SharePoint.SPFolder]) {$folders = $folder.SubFolders} if ($folder.GetType() -eq [Microsoft.SharePoint.SPFolderCollection]) {$folders = $folder} foreach ($subFolder in $folders) – SP Combatant Jan 9 '15 at 2:42

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.