Search This Blog

Monday, February 12, 2018

Download all files in document library to File-server Using SharePoint 2016 Powershell


Step1 : Save .PS1 files below script and change site-name and doc-library name
Step2 : Create .bat file and execute run with admin.

if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null)
{
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$destination = "E:\JaisonArumugam\docFolder"
$webUrl = "http://testSite"
$listUrl = "http://testsite/PhotoGallery/"

$web = Get-SPWeb -Identity $webUrl
$list = $web.GetList($listUrl)

function ProcessFolder {
    param($folderUrl)
    $folder = $web.GetFolder($folderUrl)
    foreach ($file in $folder.Files) {
        #Ensure destination directory
        $destinationfolder = $destination + "/" + $folder.Url
        if (!(Test-Path -path $destinationfolder))
        {
            $dest = New-Item $destinationfolder -type directory
        }
        #Download file
        $binary = $file.OpenBinary()
        $stream = New-Object System.IO.FileStream($destinationfolder + "/" + $file.Name), Create
        $writer = New-Object System.IO.BinaryWriter($stream)
        $writer.write($binary)
        $writer.Close()
        Write-Host "SharePoint Library Path:"$folderUrl/$file.Name
        }
}

#Download root files
ProcessFolder($list.RootFolder.Url)
#Download files in folders
foreach ($folder in $list.Folders) {
    ProcessFolder($folder.Url)
}

-----------------.bat file-------------------------


cd /d %~dp0
powershell -noexit -file "downloadFiles.PS1" "%CD%"
pause

No comments:

Post a Comment