PowerShell: Download Documents/Files from SharePoint OnPrem
$Spsite = get-spsite https://contoso.com #Replace this with Site Collection URL
$Location = "C:\destination" #Replace with Folder on file level system where you want to download the files and folders
$AllWebs = $SpSite.AllWebs
function DownloadFiles ($SiteFolder , $Web , $List)
{
$Web = Get-Spweb $Web
$list = $web.Lists.GetList($list,$true)
$items = $list.Items
foreach($item in $items)
{
$FileBinary = $item.File.OpenBinary()
$FileStream = New-Object System.IO.FileStream($SiteFolder + "/" + $item.url), Create
$FileWriter = New-Object System.IO.BinaryWriter($FileStream)
$FileWriter.write($FileBinary)
$FileWriter.Close()
}
}
function CreateSiteListFolder($Folder,$Web)
{
$SiteFolder = $Folder
$Web = Get-spweb $Web
$Lists = $Web.Lists|where{$_.BaseType -eq "DocumentLibrary" -and $_.Hidden -eq $false}
foreach($List in $Lists)
{
$ListUrl = $list.RootFolder.ServerRelativeUrl
$ListUrl = $ListUrl.Split("/")
$ListUrl = $ListUrl[$ListUrl.Count -1]
$ListUrl = $ListUrl.ToString()
$CreateListRootFolder = $SiteFolder + "/" + $ListUrl
$CreateListFolder = New-Item -Path $CreateListRootFolder -type directory -ErrorAction SilentlyContinue
$ListFolders = $List.Folders
foreach($ListFolder in $ListFolders)
{
$CreateListFolder = $SiteFolder + "/" + $ListFolder.Url
$CreateLFolder = New-Item -Path $CreateListFolder -type directory -ErrorAction SilentlyContinue
}
DownloadFiles $SiteFolder $Web.url $list.id
}
}
function Sitecreatefolder($AllWebs)
{
foreach($Web in $AllWebs)
{
if($web.IsRootWeb)
{
$RootFolder = $Location + "\" + $web.title
$CreateRootFolder = New-Item -Path $RootFolder -type directory -ErrorAction SilentlyContinue
CreateSiteListFolder $RootFolder $Web.url
}
else
{
$Folder = $Web.Url.ToString()
$Folder = $Folder.Replace($Spsite.RootWeb.Url,$RootFolder)
$CreateSiteFolder = New-Item -Path $Folder -type directory -ErrorAction SilentlyContinue
CreateSiteListFolder $Folder $Web.url
}
}
}
Sitecreatefolder $AllWebs