How Do I get the version of a file using PnP Powershell

%3CLINGO-SUB%20id%3D%22lingo-sub-156100%22%20slang%3D%22en-US%22%3EHow%20Do%20I%20get%20the%20version%20of%20a%20file%20using%20PnP%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-156100%22%20slang%3D%22en-US%22%3E%3CP%3EHi%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EI%20am%20trying%20to%20get%20the%20version%20of%20a%20file%20in%20a%20document%20library%20using%20PnP%20Powershell.%3C%2FP%3E%0A%3CP%3EConnect-PnPOnline%20-Url%20%3CURL%3E%26nbsp%3B%3C%2FURL%3E%3C%2FP%3E%0A%3CP%3E%24ListItems%20%3D%20Get-PnPListItem%20-List%20%3CMYDOCLIBRARY%3E%3C%2FMYDOCLIBRARY%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Efor%20each%20(%24item%20in%20%24ListItems)%3C%2FP%3E%0A%3CP%3E%7B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%24file%20%3D%20%24item.file%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%24fileversions%20%3D%20%24file.Versions%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20Write-Host%20%24file%2C%20%24fileversions%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%7D%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EBut%20this%20does%20not%20seem%20to%20work.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EAnybody%20any%20ideas%20%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EThanks%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENigel%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-159956%22%20slang%3D%22en-US%22%3ERe%3A%20How%20Do%20I%20get%20the%20version%20of%20a%20file%20using%20PnP%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-159956%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20can%20try%20this%20modified%20script%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EConnect-PnPOnline%20-Url%20%26lt%3Burl%26gt%3B%20%0A%24ListItems%20%3D%20Get-PnPListItem%20-List%20%26lt%3BMyDocLibrary%26gt%3B%0A%24ctx%3D%20Get-PnPContext%0Aforeach%20(%24item%20in%20%24ListItems)%0A%7B%0A%20%20%20%20%20%20%20%20%24file%20%3D%20%24item.file%0A%20%20%20%20%20%20%20%20%24ctx.load(%24file)%0A%20%20%20%20%20%20%20%20%24ctx.ExecuteQuery()%0A%20%20%20%20%20%20%20%20Write-Host%20%24file.Name%2C%24file.UIVersionLabel%0A%7D%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-158872%22%20slang%3D%22en-US%22%3ERe%3A%20How%20Do%20I%20get%20the%20version%20of%20a%20file%20using%20PnP%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-158872%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F4102%22%20target%3D%22_blank%22%3E%40Nigel%20Price%3C%2FA%3E%26nbsp%3Bsomething%20like%20the%20following%20should%20help%20you%20get%20started%2C%26nbsp%3B%20I%20have%20not%20tested%20this.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22default%20prettyprint%20prettyprinted%22%3E%3CCODE%3E%3CSPAN%20class%3D%22kwd%22%3Efor%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22kwd%22%3Eint%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20i%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20versions%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3ECount%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20i%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%26gt%3B%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3E0%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20i%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E--)%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22pun%22%3E%7B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%3CSPAN%20class%3D%22typ%22%3EFileVersion%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20version%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20file%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EVersions%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%5B%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Ei%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E-%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22lit%22%3E1%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%5D%3B%3C%2FSPAN%3E%20%20%20%0A%20%20%20%20%20%20%20%3CSPAN%20class%3D%22kwd%22%3Estring%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20label%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20version%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EVersionLabel%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%3CSPAN%20class%3D%22kwd%22%3Estring%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20id%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20version%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3EID%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EToString%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E()%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%3CSPAN%20class%3D%22kwd%22%3Estring%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20filename%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22typ%22%3EPath%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EGetFileName%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eversion%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EUrl%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%3CSPAN%20class%3D%22kwd%22%3Estring%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20ext%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22typ%22%3EPath%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EGetExtension%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E(%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3Eversion%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EUrl%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E)%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%3CSPAN%20class%3D%22kwd%22%3Estring%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20tmp1%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20version%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EIsCurrentVersion%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3B%3C%2FSPAN%3E%0A%20%20%20%20%20%20%20%3CSPAN%20class%3D%22kwd%22%3Estring%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20tmp2%20%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3D%3C%2FSPAN%3E%3CSPAN%20class%3D%22pln%22%3E%20version%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E.%3C%2FSPAN%3E%3CSPAN%20class%3D%22typ%22%3EUrl%3C%2FSPAN%3E%3CSPAN%20class%3D%22pun%22%3E%3B%3C%2FSPAN%3E%0A%20%20%20%20%3CSPAN%20class%3D%22pun%22%3E%7D%3C%2FSPAN%3E%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-158589%22%20slang%3D%22en-US%22%3ERe%3A%20How%20Do%20I%20get%20the%20version%20of%20a%20file%20using%20PnP%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-158589%22%20slang%3D%22en-US%22%3E%3CP%3EHi%26nbsp%3B%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F3058%22%20target%3D%22_blank%22%3E%40Manidurai%20Mohanamariappan%3C%2FA%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWorks%20great%20(as%20long%20as%20you%20put%20the%20t%20on%20the%20end%20of%20Get-PnPContex%20!)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWhat%20about%20if%20I%20only%20want%20the%20latest%20version%20%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERegards%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENigel%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-156478%22%20slang%3D%22en-US%22%3ERe%3A%20How%20Do%20I%20get%20the%20version%20of%20a%20file%20using%20PnP%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-156478%22%20slang%3D%22en-US%22%3E%3CP%3EYou%20can%20try%20this%20script%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EConnect-PnPOnline%20-Url%20%26lt%3Burl%26gt%3B%20%0A%24ListItems%20%3D%20Get-PnPListItem%20-List%20%26lt%3BMyDocLibrary%26gt%3B%0A%24ctx%3D%20Get-PnPContex%0Aforeach%20(%24item%20in%20%24ListItems)%0A%7B%0A%20%20%20%20%20%20%20%20%24file%20%3D%20%24item.file%0A%20%20%20%20%20%20%20%20%24fileversions%20%3D%20%24file.Versions%0A%20%20%20%20%20%20%20%20%24ctx.load(%24file)%0A%20%20%20%20%20%20%20%20%24ctx.load(%24fileversions)%0A%20%20%20%20%20%20%20%20%24ctx.ExecuteQuery()%0A%20%20%20%20%20%20%20%20Write-Host%20%24file.Name%2C%24fileversions.VersionLabel%0A%7D%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1491258%22%20slang%3D%22en-US%22%3ERe%3A%20How%20Do%20I%20get%20the%20version%20of%20a%20file%20using%20PnP%20Powershell%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1491258%22%20slang%3D%22en-US%22%3EWorked%20for%20me%3C%2FLINGO-BODY%3E
Highlighted
Regular Contributor

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 in $ListItems)

{

        $file = $item.file

        $fileversions = $file.Versions

        Write-Host $file, $fileversions

 

}

 

But this does not seem to work.

 

Anybody any ideas ?

 

Thanks

 

Nigel

5 Replies
Highlighted

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
}

 

Highlighted

Hi @Manidurai Mohanamariappan

 

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

Highlighted

@Nigel Price 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;
    }

 

Highlighted

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
}
Highlighted