8

I’ve set up a backup script in WSL to back up my Windows 11 Pro desktop to an external drive and Google Cloud Storage. However, WSL sees open Windows files as locked, causing backup issues. I think a good solution would be to create a non-persistent shadow copy of drive C:, run the backup from there, and discard the shadow copy afterward (ideally automatically after a reboot).

What’s the simplest way to create a temporary, non-persistent shadow copy of C: using PowerShell, without installing any additional software?


The command diskshadow seems promising, but I can't seem to be able to execute it on my installation.

PetaspeedBeaver
  • 574
  • 3
  • 8
  • 19

2 Answers2

9

You can create a temporary shadow copy and delete it with powershell like so:

# set drive letter
$DriveLetter = 'C'

create shadow copy of volume

$shadow = Invoke-CimMethod -MethodName Create -ClassName Win32_ShadowCopy -Arguments @{Volume="$($driveletter):\"}

get path to browse snapshot

$shadowCopy = Get-CimInstance -ClassName Win32_ShadowCopy | where {$_.ID -eq $shadow.ShadowID} $shadowMount = 'c:\temp\shadow'

create a link to access the snapshot with

cmd /c mklink /d 'c:\temp\shadow' "$($ShadowCopy.DeviceObject)"

example: copy some file out of snapshot to back it up

Copy-Item "$shadowMount\temp\test.txt" -Destination 'c:\temp\restored.txt'

delete the shadow copy when finished

$shadowCopy | Remove-CimInstance

Cpt.Whale
  • 10,914
4

The Windows Software Development Kit includes a vshadow.exe which works for this. Or at least it used to work for this in 2011 when I tried writing a VSS-based backup tool. (It was a bit clunky since XP only allowed non-persistent shadow copies so vshadow had to stay running and invoke the real backup tool.)

vshadow -vx="Microsoft Writer (Service State)" `
        -vx="Microsoft Writer (Bootable State)" 
        -vx="WMI Writer" `
        -exec=".\do_real_backup.exe" `
        -script="guid_will_be_written_here.cmd" `
        C:\

I also found a thread on StackOverflow which talks about a WMI/CIM interface:

The most direct interface is a COM+ API IVssBackupComponents, but I can't figure out how to access it via New-Object -Com if that's even possible.

grawity
  • 501,077