Download all File Versions from SharePoint using PowerShell
SharePoint is a great platform for file versioning,
and it is not uncommon that organizations wish to see or save all versions of a
document or use an earlier version to re-create the document.
Here we share a PowerShell script that downloads
all versions of all files in a SharePoint document library. You can use the
same approach for CSOM(client site object model), JSOM (JavaScript object
model) or any other SharePoint object model.
Below is the
code with explanations. Please study them before you download the script and
change the site URL and document library name to your own.
Code description
Get the SharePoint site reference.
$spSiteURL = "http://intranet.contoso.com/sites/jayant/"
$spWeb = Get-SPWeb -Identity $spSiteURL
$spSiteURL = "http://intranet.contoso.com/sites/jayant/"
$spWeb = Get-SPWeb -Identity $spSiteURL
Get the document library as folder. Here “Docs” is the name of thedocument library, and I load it as SharePoint
folder.
$spDocFolder = $spWeb.GetFolder("Docs")
$spDocFolder = $spWeb.GetFolder("Docs")
Load all files in the document library folder
$spFileCollection = $spDocFolder.Files
$spFileCollection = $spDocFolder.Files
Initialize the download object array and its
properties. It will be used to store the names and URLs of the downloaded
files.
$dlProps = @{
DownloadURL = ''
DownloadFileName = ''
}
$dlobjects = @()
$dlProps = @{
DownloadURL = ''
DownloadFileName = ''
}
$dlobjects = @()
Loop through all files in the document library and
use file object to find all versions and latest version.
ForEach($file in $spFileCollection){
ForEach($file in $spFileCollection){
The versions property returns a collection of
version objects and then loops through all versions to find version number,
download URL and file name.
$spFileVersionCollection= $file.Versions;
#===============get URLs of old versions===============
if ($spFileVersionCollection) {
ForEach($version in $spFileVersionCollection){
$downloadversURL = $version.Url;
$dlobject = New-Object -TypeName PSObject -Property $dlProps
$dlobject.DownloadURL=$downloadversURL;
$dlobject.DownloadFileName =$version.VersionLabel + "_" + $file.Name;
$dlobjects +=$dlobject;
}
#===============get URLs of old versions===============
$spFileVersionCollection= $file.Versions;
#===============get URLs of old versions===============
if ($spFileVersionCollection) {
ForEach($version in $spFileVersionCollection){
$downloadversURL = $version.Url;
$dlobject = New-Object -TypeName PSObject -Property $dlProps
$dlobject.DownloadURL=$downloadversURL;
$dlobject.DownloadFileName =$version.VersionLabel + "_" + $file.Name;
$dlobjects +=$dlobject;
}
#===============get URLs of old versions===============
The versions collection returns only the previous
versions of each file, not the current or latest version. To download the
latest version of each file you can use the ($file) file
object.
#==============get URLs of latest versions=============
$downloadlatestURL = $file.Url;
$dlobject = New-Object -TypeName PSObject -Property$dlProps
$dlobject.DownloadFileName = "Latest" +$file.UIVersionLabel + "_" + $file.Name;
$dlobject.DownloadURL=$downloadlatestURL;
$dlobjects +=$dlobject;
#==============get URLs of latest versions=============
#==============get URLs of latest versions=============
$downloadlatestURL = $file.Url;
$dlobject = New-Object -TypeName PSObject -Property$dlProps
$dlobject.DownloadFileName = "Latest" +$file.UIVersionLabel + "_" + $file.Name;
$dlobject.DownloadURL=$downloadlatestURL;
$dlobjects +=$dlobject;
#==============get URLs of latest versions=============
The $dlobjects array
has the object of all file names and URLs to download and can be used to
download all files with the System.Net.WebClient method to download files.
#================download version files================
$destination="C:\Users\jayant\Desktop\version files\"
$webclient = New-Object System.Net.WebClient
$webclient.UseDefaultCredentials = $true
ForEach($dlobj in $dlobjects){
$fullURL = $spSiteURL + $dlobj.DownloadURL;
$destinationFullPath=$destination +$dlobj.DownloadFileName;
$webclient.DownloadFile($fullURL,$destinationFullPath);
}
#================download version files================
#================download version files================
$destination="C:\Users\jayant\Desktop\version files\"
$webclient = New-Object System.Net.WebClient
$webclient.UseDefaultCredentials = $true
ForEach($dlobj in $dlobjects){
$fullURL = $spSiteURL + $dlobj.DownloadURL;
$destinationFullPath=$destination +$dlobj.DownloadFileName;
$webclient.DownloadFile($fullURL,$destinationFullPath);
}
#================download version files================
The whole code
$spSiteURL = "http://intranet.contoso.com/sites/jayant/"
$spWeb = Get-SPWeb -Identity $spSiteURL
$spDocFolder = $spWeb.GetFolder("Docs")
$spFileCollection = $spDocFolder.Files
$dlProps =
@{
DownloadURL =
''
DownloadFileName =
''
}
$dlobjects = @()
ForEach($file
in $spFileCollection){
$spFileVersionCollection= $file.Versions;
#================this
to get old versions urls==========
if ($spFileVersionCollection) {
ForEach($version
in $spFileVersionCollection){
$downloadversURL
= $version.Url;
$dlobject
= New-Object
-TypeName PSObject
-Property $dlProps
$dlobject.DownloadURL=$downloadversURL;
$dlobject.DownloadFileName =
$version.VersionLabel
+ "_"
+ $file.Name;
$dlobjects
+=$dlobject;
}
}
#================this
to get old versions urls==========
#================this
to get latest versions urls==========
$downloadlatestURL
= $file.Url;
$dlobject
= New-Object
-TypeName PSObject
-Property $dlProps
#$urlstoDownload.DownloadURL
+=$downloadlatestURL;
$dlobject.DownloadFileName =
"Latest" +
$file.UIVersionLabel
+ "_"
+ $file.Name;
$dlobject.DownloadURL=$downloadlatestURL;
$dlobjects
+=$dlobject;
#================this
to get latest versions urls==========
}
#====================download version
files============================
$destination="C:\Users\jayant\Desktop\version files\"
$webclient = New-Object System.Net.WebClient
$webclient.UseDefaultCredentials
= $true
ForEach($dlobj
in $dlobjects){
$fullURL
= $spSiteURL
+ $dlobj.DownloadURL;
$destinationFullPath=$destination
+ $dlobj.DownloadFileName;
$webclient.DownloadFile($fullURL, $destinationFullPath);
}
#====================download version
files============================
Comments
Post a Comment