Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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
add comment

migrated from stackoverflow.com Apr 6 '13 at 14:20

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

1 Answer

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

eg.

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
       GetFiles($Subfolder)
   }
 }



$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
                }

                GetFiles($list.RootFolder)
            }
        }
    }    
}
share|improve this answer
add comment

Your Answer

 
discard

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