I'm using OneDrive (office 365 account) as a cloud backup, and I got a problem with the versioning system as for big files (outlook .ost and .pst mainly) it keeps generating new versions for the file with each synchronization thus reaching easily the storage account limit.

I wasn't able to find a way to limit the (Major) versioning number under 100 (do you have any suggestion other than Microsoft article)

Anyway, I came across this wonderful solution/code to control the number of versions of a file/folder and I adapted it for my scenario.

It works absolutely fine, BUT when the file is big (let say > 1GB), ALWAYS get this error trying to get its versions.

Operation is not valid due to the current state of the object.

No matter how I access the file versions it gets an error. I tried several approaches:

  • $File.Versions
  • Get-PnPProperty -ClientObject $File -Property Versions
  • Using context...
   $Ctx = Get-PnPContext 
   $Ctx.RequestTimeOut = -1

But I can't get it right. I'm currently using PnP PowerShell .NET Core version, but the same happens with previous versions.

enter image description here

And it has versions... enter image description here

Note: I can still manage/delete the versions by web interface, but not by code

3 Answers 3


Since no one seems to know a solution, I ended up with a workaround for my big files history problem.

Surprisingly, although the big files versions can't be reached, they can be completely erased.

write-host -f Cyan "`t`tDeleting ALL versions..." -NoNewline
try {
      Write-Host -f Green " DELETED"
catch {
      Write-Host -f Red " ERROR!!!"

Keeping the onedrive account use at bay.

Note: I still would apreciate a more neat solution regarding keeping small number of versions.

  • I'm having same problem, although I am using CSOM commands. Not even '...DeleteAll()' works for me. Files bigger than ~1.2GB always return this error message, although upload of new version works without a problem. It's been same time since you posted this answer, have you found new ways how to do it ?
    – mauek unak
    Feb 3, 2021 at 15:33
  • 1
    Sorry @mauekunak , nothing new under the sun ... same old, same old. I have a try - catch to get the file versions. If it fails, I asume the file is a large one and then, with this above piece of code, "reset" it's versions history. It is working for me using github.com/pnp/powershell Feb 4, 2021 at 17:35
  • so after further investigation I found out that it was problem with permissions of file(s) on SharePoint. The error message is not very intuitive.
    – mauek unak
    Feb 4, 2021 at 20:26

I am having the same issue and it seems that this is coming from SharePoint when using PNP or CSOM. However, this works fine when using REST. Hence, you can overcome this using REST as follows

Connect-PnPOnline https://test.sharepoint.com -Interactive    
$versionsRest =  Invoke-PnPSPRestMethod -Url "/_api/web/lists/getByTitle('ListTitle')/items('ItemId')/versions" -ErrorAction Stop
$versions = $versionsRest.value


$versionsRest =  Invoke-PnPSPRestMethod -Url "/_api/web/lists/getByTitle('Shared Documents')/items('10')/versions" -ErrorAction Stop
$versions = $versionsRest.value

You can easily change the versioning settings on Document library for your OneDrive.

  1. Log on your OneDrive site and add into URL _layouts/15/viewlsts.aspx?view=14 so it will looks like: https://***-my.sharepoint.com/personal/login_domain_com/_layouts/15/viewlsts.aspx?view=14
  2. Click on Documents and Settings
  3. Click Versioning settings
  4. Change the number for "Keep the following numbers of major version" (or/and for "Keep drafts for the following number of major versions").
  5. It will automatically delete rest of the versions. For instance you can set just 5 or 10. It will affect all documents in this library. But you can definitely save lots of space on OneDrive.

enter image description here

Hope this helps! :) If so, please mark it as answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.