Requirement:
Get the complete structure of all folders-subfolder-files from a SharePoint document library and export to CSV file.
PowerShell Script to Iterate through each folder and Sub-folder and get all files:
Get the complete structure of all folders-subfolder-files from a SharePoint document library and export to CSV file.
PowerShell Script to Iterate through each folder and Sub-folder and get all files:
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinueThis exports the inventory of all files and folders structure to a CSV file!
#Custom Function to get all files of a folder
Function GetFiles-ByFolder([Microsoft.SharePoint.SPFolder]$Folder)
{
write-host "Processing Folder:"$Folder.URL
Foreach($File in $Folder.Files)
{
$content = $Folder.Name + "," + $Folder.URL +"," + $File.Name
Add-content $OutPutFile $content
Write-host $content
}
}
#Variables
$WebURL="https://portal.crescent.com/sites/sales/"
$ListName="Team Docs"
$OutPutFile = "C:\LibraryFiles.csv"
#Delete the file if exists
If (Test-Path $OutPutFile) { Remove-Item $OutPutFile }
#Write CSV headers
Add-Content $OutPutFile "Root Folder, URL, File Name"
#Get site object
$Web = Get-SPWeb $WebURL
$List = $Web.Lists[$ListName]
$Folder = $List.RootFolder
#Call the function for Root folder
GetFiles-ByFolder $Folder
#Call the function for each subfolder - Excluding "Forms"
$folder.SubFolders | Where {$_.Name -ne "Forms" } | foreach-Object {
#Call the function Recursively!
GetFiles-ByFolder $_
}