Tuesday, February 21, 2017

Get folder and file names from SharePoint SiteCollection using PowerShell

Get folder and file names from SharePoint SiteCollection using PowerShell
Step1: Copy below code into notepad and save as 'getFiles.ps1' and save.
--------------------------------------
Start-SPAssignment -Global | Out-Null
function Get-SPWebs($SiteCollection)
{
       $SiteCollection = Get-SPSite $SiteCollection
       $webs = @()
       $SiteCollection.allwebs | %{$webs += $_.url}
       return $webs
}
function Get-SPFolders($webs)
{
       foreach($web in $webs)
       {
              $web = Get-SPWeb $web
              Write-Host "`n$($web.url)" -ForegroundColor Green
              Add-Content C:\log01.txt $($web.url)
              $lists = $web.lists | ?{$_.Hidden -eq $false}
              #$lists = $web.lists | ?{$_.itemcount -ge "1" -And $_.Hidden -eq $false -And $_.BaseType -eq "DocumentLibrary"}
              #$lists = $web.lists | ?{$_.title -eq "Documents" -and $_.itemcount -ge "1" -And $_.BaseType -eq "DocumentLibrary"} #Change any identifier here
              foreach($list in $lists)
              {                   
                     Write-Host "- $($list.RootFolder.url)"
                  Add-Content C:\log01.txt "  - $($list.RootFolder.url)"
                     $rootfolder = $web.GetFolder($list.RootFolder.Url)
                     Get-SPFileNames($rootfolder)
                     foreach($folder in $list.folders)
                     {
                           $folder = $web.GetFolder($folder.url)
                           Get-SPFileNames($folder)
                     }
              }
              $web.dispose()
       }
}
function Get-SPFileNames($folder)
{
       foreach($file in $folder.Files)
       {            
              Add-Content C:\log01.txt "        - $($file.Url)"
       }
}
$Sitecollection = "http://SharePointURL:1234"
$webs = Get-SPWebs -SiteCollection $Sitecollection
Get-SPFolders -Webs $webs
Stop-SPAssignment -Global
--------------------------------------
Step2: Open 'SharePoint Management Shell' as 'Run as administrator' -> open 'getFiles.ps1' and click enter to execute script.
Step3: OutPut: we can similar out as below.
--------------------------------------
http://SharePointSiteURL:1234
   - DocLib1
        - DocLib1/Doc2.txt
        - DocLib1/Doc1.rtf
        - DocLib1/folder1/CustomList1.JPG
        - DocLib1/folder2/Bootstrap.JPG
  - Shared Documents
        - Shared Documents/ImageSlider/4.jpg
        - Shared Documents/ImageSlider/7.jpg
  - SitePages
        - SitePages/ImageSlider.aspx
  - Style Library
        - Style Library/Media Player/MediaWebPartPreview.png
  - Lists/test
        - Lists/test/DispForm.aspx
        - Lists/test/EditForm.aspx
        - Lists/test/AllItems.aspx
        - Lists/test/view1.aspx
        - Lists/test/NewForm.aspx
-------------------------------------- 

No comments:

Post a Comment

Featured Post

Mention a Channel or Team – Power Automate

Mention a Channel or Team – Power Automate graph.microsoft.com/v1.0/teams/{teamId}/channels/{channelId}/messages Channel: ---------- {   &qu...

Popular posts