Forum Discussion
Nigel_Price9911
Feb 12, 2018Iron Contributor
How Do I get the version of a file using PnP Powershell
Hi
I am trying to get the version of a file in a document library using PnP Powershell.
Connect-PnPOnline -Url <url>
$ListItems = Get-PnPListItem -List <MyDocLibrary>
for each ($item...
Manidurai Mohanamariappan
Feb 13, 2018Iron Contributor
You can try this script
Connect-PnPOnline -Url <url> $ListItems = Get-PnPListItem -List <MyDocLibrary> $ctx= Get-PnPContex foreach ($item in $ListItems) { $file = $item.file $fileversions = $file.Versions $ctx.load($file) $ctx.load($fileversions) $ctx.ExecuteQuery() Write-Host $file.Name,$fileversions.VersionLabel }
- Nigel_Price9911Feb 14, 2018Iron Contributor
Works great (as long as you put the t on the end of Get-PnPContex !)
What about if I only want the latest version ?
Regards
Nigel
- Dean_GrossFeb 14, 2018Silver Contributor
Nigel_Price9911 something like the following should help you get started, I have not tested this.
for (int i = versions.Count; i > 0; i--) { FileVersion version = file.Versions[i - 1]; string label = version.VersionLabel; string id = version.ID.ToString(); string filename = Path.GetFileName(version.Url); string ext = Path.GetExtension(version.Url); string tmp1 = version.IsCurrentVersion; string tmp2 = version.Url; }
- Manidurai MohanamariappanFeb 16, 2018Iron Contributor
You can try this modified script
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 }
- Poonam_AmbeJun 26, 2020Copper ContributorWorked for me