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%3CLINGO-SUB%20id%3D%22lingo-sub-1700967%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-1700967%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%3B%3CSPAN%3EFor%20SharePoint%20Online%20I'm%20using%20following%20code%3A%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%20%20%20%20Get-PnPFile%20-Url%20%24item.ServerRelativeUrl%20-Path%20%24destinationFolderPath%20-AsFile%20-Force%20%23%20Latest%20version%0A%20%20%20%20%24ctx%3D%20Get-PnPContext%0A%20%20%20%20%24ctx.Load(%24item.Versions)%0A%20%20%20%20%24ctx.ExecuteQuery()%0A%20%20%20%20foreach%20(%24version%20in%20%24item.Versions)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%24versionValue%20%3D%20%24version.VersionLabel%0A%20%20%20%20%20%20%20%20%24str%20%3D%20%24version.OpenBinaryStream()%0A%20%20%20%20%20%20%20%20%24ctx.ExecuteQuery()%0A%20%20%20%20%20%20%20%20%24filename%20%3D%20%20(Split-Path%20%24item.ServerRelativeUrl%20-Leaf)%20%2B%20%22.%22%20%2B%20%24versionValue%0A%20%20%20%20%20%20%20%20%24filepath%20%3D%20Join-Path%20%24destinationFolderPath%20%24filename%0A%20%20%20%20%20%20%20%20%24fs%20%3D%20New-Object%20IO.FileStream%20%24filepath%20%2C'Append'%2C'Write'%2C'Read'%0A%20%20%20%20%20%20%20%20%24str.Value.CopyTo(%24fs)%20%23%20Older%20version%0A%20%20%20%20%20%20%20%20%24fs.Close()%0A%20%20%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1788991%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-1788991%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F560544%22%20target%3D%22_blank%22%3E%40AlyaKoni%3C%2FA%3E%26nbsp%3BWorks%20like%20a%20charm%2C%20thank%20you!%3C%2FP%3E%3C%2FLINGO-BODY%3E
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

7 Replies

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
}

 

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

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

 

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
}

@Nigel Price For SharePoint Online I'm using following code:

 

    Get-PnPFile -Url $item.ServerRelativeUrl -Path $destinationFolderPath -AsFile -Force # Latest version
    $ctx= Get-PnPContext
    $ctx.Load($item.Versions)
    $ctx.ExecuteQuery()
    foreach ($version in $item.Versions)
    {
        $versionValue = $version.VersionLabel
        $str = $version.OpenBinaryStream()
        $ctx.ExecuteQuery()
        $filename =  (Split-Path $item.ServerRelativeUrl -Leaf) + "." + $versionValue
        $filepath = Join-Path $destinationFolderPath $filename
        $fs = New-Object IO.FileStream $filepath ,'Append','Write','Read'
        $str.Value.CopyTo($fs) # Older version
        $fs.Close()
    }

@AlyaKoni Works like a charm, thank you!