How to find and open previous versions of a folder programmatically (using PowerShell, WMI, etc)?

Solution 1:

The volrest utility, available from the Windows Server 2003 Resource Kit Tools, can be used to list the previous versions of a folder. It worked for me on Windows 7 and should still work on Windows 8. Just be careful with your parameters, since it can also restore previous versions.

An example of use (from the below-quoted link):

C:\>volrest "\\test220\reports\Annual Reports 2004\doc.4.rtf"

VOLREST 1.1 - Previous Version command-line tool
(C) Copyright 2003 Microsoft Corp.

 Searching previous versions on \\test220\reports\annual report 2004\doc.4.rtf

07/01/2004  01:28 PM    37,786 \\test220\reports\@GMT-2004.07.01-18.34.35\annual 
                           report 2004\doc.4.rtf
07/01/2004  01:27 PM    37,740 \\test220\reports\@GMT-2004.07.01-18.28.02\annual 
                           report 2004\doc.4.rtf
07/01/2004  11:47 AM    37,690 \\test220\reports\@GMT-2004.07.01-18.24.41\annual 
                           report 2004\doc.4.rtf

            3 File(s)  113,216 bytes
            0 Dir(s)

With the output of this program, maybe used with the /B parameter for bare format, you should be able to construct a script that will explore one of the listed versions.

For more info see : Windows Server Hacks: Restoring Shadow Copies Using the Command Line.

Solution 2:

Here's an example of how to use volrest in a batch file to find the path to the newest and oldest shadow copies on a local or remote server. The paths returned can be used directly without the need to use mklink. Tested locally on Windows Server 2019 and remotely from Windows 10.

:: Proof of concept for finding shadow copy paths on a local or remote server.
:: Requires volrest.exe from the Windows Server 2003 Resource Kit Tools:
:: https://www.microsoft.com/en-us/download/details.aspx?id=17657

@ECHO OFF
SET SERVER=LOCALHOST

:: Loop through output of volrest run against a test folder.  Note that volrest returns
:: items in reverse chronological order and skips shadow copies if the folder hasn't
:: changed between shadow copies.  I found that the Windows\Temp folder was updated
:: frequently enough for my purposes.
::
:: If we're run after the last shadow copy but BEFORE the Windows\Temp folder has been 
:: updated, then volrest won't list the latest shadow copy.  We can fix this by writing a 
:: dummy file to the Windows\Temp folder first and then deleting it.  Uncomment the 
:: following two commands if this might be an issue for you.

:: Optional code to force a dummy update to ensure latest shadow copy is found
:: TYPE NUL > \\%SERVER%\C$\Windows\Temp\ShadowCopyFind.tmp
:: DEL \\%SERVER%\C$\Windows\Temp\ShadowCopyFind.tmp

FOR /F "TOKENS=3 DELIMS=\" %%A IN ('volrest.exe /B \\%SERVER%\C$\Windows\Temp*') DO (
    IF NOT DEFINED GMT_TOKEN_NEWEST SET GMT_TOKEN_NEWEST=%%A
    SET GMT_TOKEN_OLDEST=%%A
)

ECHO Path to Newest Shadow Copy: \\%SERVER%\C$\%GMT_TOKEN_NEWEST%
ECHO Path to Oldest Shadow Copy: \\%SERVER%\C$\%GMT_TOKEN_OLDEST%

Sample output:

Path to Newest Shadow Copy: \\LOCALHOST\C$\@GMT-2020.05.05-16.30.02
Path to Oldest Shadow Copy: \\LOCALHOST\C$\@GMT-2020.03.20-22.30.01

I use this as part of a nightly robocopy job that I've scheduled to run after my Server's last shadow copy of the day. This lets me leverage the Previous Versions shadow copies my server is already creating, ensures that my robocopy job doesn't encounter any locked files, and provides a true "point in time" consistency.