none
Get all Document libraries /Folder/items in a site collection using power shell

    Question

  • Hi,

    I want to loop through site collection and want to get all document libraries, plus all documents, folder and document under folder.

    i just want to main list.

    script reference: http://sharepointrelated.com/2011/11/28/get-all-sharepoint-lists-by-using-powershell/

    Just add document library check.

    Through following script i am able to loop through all library and list document. But problem is that is missout folder. i want to list folder then list document with folder. if folder are nested then they also get listed.

    Pls guide how to move further.

    param
    (
    [string] $URL = "http://SPServer",
    [boolean] $WriteToFile = $true
    )

    #Get all lists in farm
    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

    #Counter variables
    $webcount = 0
    $listcount = 0

    if($WriteToFile -eq $true)
    {
    $outputPath = Read-Host "Outputpath (e.g. C:\directory\filename.txt)"
    }
    if(!$URL)
    {
    #Grab all webs
    $webs = (Get-SPSite -limit all | Get-SPWeb -Limit all -ErrorAction SilentlyContinue)
    }
    else
    {
    $webs = Get-SPWeb $URL
    }
    if($webs.count -ge 1 -OR $webs.count -eq $null)
    {
        foreach($web in $webs)
        {

       #Grab all lists in the current web
        $lists = $web.Lists    

            Write-Host "Website"$web.url -ForegroundColor Green  
                if($WriteToFile -eq $true){Add-Content -Path $outputPath -Value "Website $($web.url)"}
                    foreach($list in $lists)
                {

                if($list.BaseType -eq "DocumentLibrary")

                
                            {

                    $listcount +=1   
                    Write-Host " – "$list.Title           


                    if($WriteToFile -eq $true){Add-Content -Path $outputPath -Value " – $($list.Title)"}

                        
                foreach ($listItem in $list.Items)
                    {
                        #display the filename of the document
                        write-host $listItem.Name

                         if($WriteToFile -eq $true)
                            {
                                Add-Content -Path $outputPath -Value "         -> $($listItem.Name)"

                            }

                }
                          }

                }
            $webcount +=1
            $web.Dispose()
         
       }


    #Show total counter for checked webs & lists
    Write-Host "Amount of webs checked:"$webcount
    Write-Host "Amount of lists:"$listcount
    }
    else
    {
    Write-Host "No webs retrieved, please check your permissions" -ForegroundColor Red -BackgroundColor Black
    }

    thx


    iffi

    Monday, January 28, 2013 10:43 AM

Answers

  • Here is the fix: Just Move the Function to Top.

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    
    Function GetFiles($Folder)
    { 
       Write-Host "+"$Folder.Name
    
        foreach($file in $Folder.Files)
    	{	
    	    Write-Host "`t" $file.Name
    	}
    	 #Loop through all subfolders and call the function recursively
         foreach ($SubFolder in $Folder.SubFolders)
            {
    		    if($SubFolder.Name -ne "Forms")
    		    {  
    			    Write-Host "`t" -NoNewline
    				GetFiles($Subfolder)
    				 
    			}
    		}
     }
    
    #Get the Site collection 
    $Site= Get-SPSite "http://sharepoint.com" 
           #Loop throuh all Sub Sites
           foreach($Web in $Site.AllWebs)
           {
    	    Write-Host "-----------------------------------------------------"
    	    Write-Host "Site Name: '$($web.Title)' at $($web.URL)"
    		Write-Host "-----------------------------------------------------"
    	    foreach($list in $Web.Lists)
    		{
    		   #Filter Doc Libs, Eliminate Hidden ones
    			if(($List.BaseType -eq "DocumentLibrary") -and ($List.Hidden -eq $false) )
                   { 
    			    GetFiles($List.RootFolder)
    			   }
    		}
    	   }
    	   
    

    • Marked as answer by imughal Tuesday, January 29, 2013 10:09 AM
    Tuesday, January 29, 2013 7:37 AM

All replies

  • Hello,

    Here is the PowerShell script which can get you Files-Folders-Libraries:

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    
    #Get the Site collection 
    $Site= Get-SPSite "http://sharepoint.com" 
           #Loop throuh all Sub Sites
           foreach($Web in $Site.AllWebs)
           {
    	    Write-Host "-----------------------------------------------------"
    	    Write-Host "Site Name: '$($web.Title)' at $($web.URL)"
    		Write-Host "-----------------------------------------------------"
    	    foreach($list in $Web.Lists)
    		{
    		   #Filter Doc Libs, Eliminate Hidden ones
    			if(($List.BaseType -eq "DocumentLibrary") -and ($List.Hidden -eq $false) )
                   { 
    			    GetFiles($List.RootFolder)
    			   }
    		}
    	   }
    	   
    Function GetFiles($Folder)
    { 
       Write-Host "+"$Folder.Name
    
        foreach($file in $Folder.Files)
    	{	
    	    Write-Host "`t" $file.Name
    	}
    	 #Loop through all subfolders and call the function recursively
         foreach ($SubFolder in $Folder.SubFolders)
            {
    		    if($SubFolder.Name -ne "Forms")
    		    {  
    			    Write-Host "`t" -NoNewline
    				GetFiles($Subfolder)
    				 
    			}
    		}
     }

    Regards,

    Sal

    Monday, January 28, 2013 1:04 PM
  • Hi,

    i run script and getting following error:

    The term 'GetFiles' is not recognized as the name of a cmdlet, function, script
     file, or operable program. Check the spelling of the name, or if a path was in
    cluded, verify that the path is correct and try again.
    At D:\sharepoint_media\Get_Folder_List.ps1:16 char:16
    +                 GetFiles <<<< ($List.RootFolder)
        + CategoryInfo          : ObjectNotFound: (GetFiles:String) [], CommandNot
       FoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    pls guide.

    thx


    iffi

    Tuesday, January 29, 2013 5:42 AM
  • Here is the fix: Just Move the Function to Top.

    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    
    Function GetFiles($Folder)
    { 
       Write-Host "+"$Folder.Name
    
        foreach($file in $Folder.Files)
    	{	
    	    Write-Host "`t" $file.Name
    	}
    	 #Loop through all subfolders and call the function recursively
         foreach ($SubFolder in $Folder.SubFolders)
            {
    		    if($SubFolder.Name -ne "Forms")
    		    {  
    			    Write-Host "`t" -NoNewline
    				GetFiles($Subfolder)
    				 
    			}
    		}
     }
    
    #Get the Site collection 
    $Site= Get-SPSite "http://sharepoint.com" 
           #Loop throuh all Sub Sites
           foreach($Web in $Site.AllWebs)
           {
    	    Write-Host "-----------------------------------------------------"
    	    Write-Host "Site Name: '$($web.Title)' at $($web.URL)"
    		Write-Host "-----------------------------------------------------"
    	    foreach($list in $Web.Lists)
    		{
    		   #Filter Doc Libs, Eliminate Hidden ones
    			if(($List.BaseType -eq "DocumentLibrary") -and ($List.Hidden -eq $false) )
                   { 
    			    GetFiles($List.RootFolder)
    			   }
    		}
    	   }
    	   
    

    • Marked as answer by imughal Tuesday, January 29, 2013 10:09 AM
    Tuesday, January 29, 2013 7:37 AM
  • Hi,

    thanks, i did and it worked. is it also possible to miss out site assets library too.

    thx


    iffi

    Tuesday, January 29, 2013 10:09 AM
  • Yes, Just change the IF condition as:

    if( ($SubFolder.Name -ne "Forms") -and ( $SubFolder.Name -ne "Site Assets") )

    Regards,

    Sal

    Tuesday, January 29, 2013 11:39 AM
  • Just one suggestion before excluding all "Forms" folder:

    There may be a case that one of the sub-folders of Document Library contains a folder named as Forms. 

    e.g. Document Library > Sub Folder 1 > Sub Folder 2 > Forms > Filename.doc

    So to include the above "Forms" folder, we should add PowerShell equivalent of

    if (!(SubFolder.Name.Equals("Forms") && SubFolder.ParentFolder.Url == SubFolder.DocumentLibrary.RootFolder.Url))

    Friday, February 08, 2013 10:52 AM
  • very useful script...i also found one here with a little bit more options...to output a csv file with library, foldername and filename indicator

    #Get all lists in farm
    Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

    #Get the Site collection
    $Site= Get-SPSite http://sharepointsite

    $outputPath = “c:\Portallist.csv”

    Function GetMyFiles($Folder)
    {

    Write-Host “+in : “$Folder.Name
    foreach($file in $Folder.Files)
    {
    Write-Host “`t” $file.Name
    Add-Content -Path $outputPath -Value  “                     File Name : $($file.Name)”

    }

    #Loop through all subfolders and call the function recursively
    foreach ($SubFolder in $Folder.SubFolders)
    {
    if($SubFolder.Name -ne “Forms”)
    {
    Write-Host “`t” -NoNewline
    Add-Content -Path $outputPath -Value  “           Folder Name : $($SubFolder.Name)”
    GetMyFiles($Subfolder)

    }
    }

    }
    #Loop throuh all Sub Sites
    foreach($Web in $Site.AllWebs)
    {
    Write-Host “—————————————————–”
    Write-Host “Site Name: ‘$($web.Title)’ at $($web.URL)”
    Add-Content -Path $outputPath -Value  “Website : $($web.Title)”
    foreach($list in $Web.Lists)
    {
    #Filter Doc Libs, Eliminate Hidden ones
    if(($List.BaseType -eq “DocumentLibrary”) -and ($List.Hidden -eq $false) )
    {
    Write-Host “——————————————-” “+” $List.RootFolder
    Add-Content -Path $outputPath -Value  “         Library : $($List.RootFolder)”
    GetMyFiles($List.RootFolder)

    }
    }
    }

    Tuesday, February 26, 2013 2:57 PM