Restore file version history


The PowerShell script presented here copies the latest files of the file version history to any folder or to another computer. In the copy, the date is removed from the file name, thus restoring the original file names.

The file version history can be managed using Windows GUI on the computer where the file version history is enabled and configured.

More information about file version history: www.libe.net Windows file version history.

The data is completely present in the file system of the backup disk, but, as mentioned at the beginning, it has a date:

To make it easy to remove the date from the folders and files, I wrote a small script that copies the files to any folder, ignoring the date.

The following script is a very simple implementation, it overwrites the files in the target if multiple versions exist. Since the gci (Get-ChildItem) function calls the data sorted by date, at least in my tests, the newest file version should end up in the destination folder.

When starting the script, any subfolder of the FileHistory folder can be selected.

Source code

[+]
function Read-FolderBrowserDialog([string]$Message, [string]$InitialDirectory, [switch]$NoNewFolderButton){
    $browseForFolderOptions = 0
    if ($NoNewFolderButton) { 
        $browseForFolderOptions += 512 
    }

    $app = New-Object -ComObject Shell.Application
    $folder = $app.BrowseForFolder(0, $Message, $browseForFolderOptions, $InitialDirectory)
    if ($folder) { 
    $selectedDirectory = $folder.Self.Path } else { $selectedDirectory = '' 
    }
    [System.Runtime.Interopservices.Marshal]::ReleaseComObject($app) > $null
    return $selectedDirectory
}
#select Backup Folder
do{
    $filehistorypath = Read-FolderBrowserDialog -Message "please select History Folder: FileHistory\Username\Computername\Data\Drive ..." -NoNewFolderButton
    if (![string]::IsNullOrEmpty($filehistorypath)) { 
        Write-Host "You selected the directory: $filehistorypath" 
    } else { 
        "You did not select a directory." 
    }
    if ($filehistorypath.contains('\Data\C')) {
        echo 'Path contains: \Data\C'
    } else {
        echo 'Path does not contain: \Data\C'}
} until ($filehistorypath.contains('\Data\C'))
#Select Restore Folder
$restorepath=Read-FolderBrowserDialog -Message "please select a Restore Path" -NoNewFolderButton
if (![string]::IsNullOrEmpty($restorepath)) { 
    Write-Host "You selected the directory: $restorepath" 
} else {
    [Environment]::Exit(1) 
}

$Files=gci -Path $filehistorypath -Force -Recurse
for($i = 0; $i -lt $Files.Count; $i++){
    $restorepath1= $Files[$i].FullName -Split '\\Data\\C'
    $restorepath2= $restorepath1[1] -Split ' \(20'
    echo ($restorepath2[1] + " --> " +$restorepath2[0])
    $targetpath=split-path ($restorepath + $restorepath2[0] + $Files[$i].Extension)
    if (!(Test-Path $targetpath)) {
        mkdir $targetpath
    }
    Copy-Item ($Files[$i].FullName) ($restorepath + $restorepath2[0] + $Files[$i].Extension) -force 
}

The function Read-FolderBrowserDialog comes from this page: http://blog.danskingdom.com

Run script

Simply copy the source code into a Windows Powershell Window:

In the next dialog the restore destination:

Afterwards Powershell copies all data from the backup folder (FileHistory) to the restore folder

positive Bewertung({{pro_count}})
Rate Post:
{{percentage}} % positive
negative Bewertung({{con_count}})

THANK YOU for your review!

Publication: 2022-12-01 from Bernhard 🔔


Top articles in this section


PowerShell Log-Files: Logging into a textfile - write to file
Log files in PowerShell can be created via the Out-File command, via a custom function, or via PowerShell's built-in Transcript.

PowerShell Loops and Array
An array stores multiple values, similar to a 2-column table.

PowerShell text file and csv read / write
PowerShell text file and csv read / write

Questions / Comments