How to restore corrupted files automatically?

Michael Schmidt 360 Reputation points
2025-02-23T14:14:01.9566667+00:00

Hi,

Some Files have been corrupted during an OneDrive-Sync - they are shown as having 0 Bytes, and the header can't be read. Because of this these files do not have a preview (some files as examples):

0

And online they are shown as having no filesize (second column, some files as examples):

2

However, I can restore a previous version in OneDrive - manually. I did not this version myself. It happened during sync. (some file as example):

3

Then the file has size and works again - online and offline (some file as example):

4

The image-files aren't only in one folder - but I've noticed this issue in several folders. It's only image-files and only the ones I sync with OneDrive.

My questions are:

How can I automate the process of restoring a former version automatically if the size of an image file on my desktop is 0 bytes?

As syncing to OneDrive itself caused the problem: Does Microsoft offer a solution itself?

Bye

Michael

OneDrive Management
OneDrive Management
OneDrive: A Microsoft file hosting and synchronization service.Management: The act or process of organizing, handling, directing or controlling something.
1,404 questions
0 comments No comments
{count} votes

Accepted answer
  1. Emi Zhang-MSFT 29,206 Reputation points Microsoft External Staff
    2025-02-28T08:20:27.3966667+00:00

    Hi,

    You can try to create a PowerShell script to check the file size and restore a previous version if the file size is 0 bytes. Here's a basic example:

    $filePath = "C:\Users\YourUsername\OneDrive\YourImageFile.jpg"
    $fileSize = (Get-Item $filePath).length
    
    if ($fileSize -eq 0) {
        # Restore previous version using OneDrive's version history
        # This part will require OneDrive API or a third-party tool to automate
        Write-Output "File size is 0 bytes. Restoring previous version..."
        # Add your code to restore the previous version here
    } else {
        Write-Output "File size is not 0 bytes. No action needed."
    }
    

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Emi Zhang-MSFT 29,206 Reputation points Microsoft External Staff
    2025-02-24T08:14:25.6933333+00:00

    Hi,

    If you're a Microsoft 365 subscriber, you can try to restore your entire OneDrive to a specific date. Sign in to the OneDrive website, select Settings > Options > Restore your OneDrive, and then select a date to restore.

    Please NOTE that you can try this method if you don't have any new files, and if you have updated files, it is recommended to back up these files before trying this method:

    https://support.microsoft.com/en-us/office/restore-a-previous-version-of-a-file-stored-in-onedrive-159cad6d-d76e-4981-88ef-de6e96c93893

    Hope this suggestion will help you.


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.