You can use the below PnP PowerShell code:
Connect-PnPOnline -Url <url>
$ListItems = Get-PnPListItem -List <MyDocLibrary>
$ctx= Get-PnPContext
foreach ($item in $ListItems)
{
$file = $item.file
$ctx.load($file)
$ctx.ExecuteQuery()
Write-Host $file.Name,$file.UIVersionLabel
}
Source of the above code:
How Do I get the version of a file using PnP Powershell
Other Approaches:
Download all version of a file using the PowerShell and SharePoint web service (Versions.asmx):
# ******* Variables Section ******************
#Define these variables
$WebURL="http://sharepoint.crescent.com/sites/Operations/"
$FilePath ="http://sharepoint.crescent.com/sites/Operations/docs/designDoc.docx"
$TargetFolder = "C:\Downloads"
# *********************************************
#Web Service URL
$WebServiceURL =$WebURL+"_vti_bin/Versions.asmx"
$WebService = New-WebServiceProxy -Uri $WebServiceURL -UseDefaultCredential
$WebService.URL=$WebServiceURL
#Get File name from file path
$FileName = $FilePath.Substring($FilePath.LastIndexOf("/")+1,($FilePath.Length-$FilePath.LastIndexOf("/")-1))
#Create the Target Library if it doesn't exists
if (!(Test-Path -path $TargetFolder))
{
#If it doesn't exists, Create
$TargetFolder = New-Item $TargetFolder -type directory
}
#Call web service method "GetVersions" to retrieve versions collection
$FileVersions = $WebService.GetVersions($FilePath).Result
foreach($File in $FileVersions)
{
#Frame the File name : E.g. 1.0_Filename.ext
$VersionFileName = "$($TargetFolder)\$($File.version)_$($FileName)"
write-host $VersionFileName
$webclient = New-Object System.Net.WebClient
$webclient.UseDefaultCredentials = $true
write-host $File.url
$webclient.DownloadFile($File.url,$VersionFileName)
Write-Host "Downloaded version: $($File.Version)"
}
Source of the above code:
Download All Versions of a Document in SharePoint using PowerShell - Web Services
Download all versions of a file using the SharePoint PowerShell server object model:
Get-PSSnapin -Registered
if(-not (Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue)){Add-PSSnapin "Microsoft.SharePoint.PowerShell"}
$spSiteURL = "https://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;
#===============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===============
#================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==========
}
#================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================
Source of the above code:
Download all File Versions from SharePoint