Microsoft - Programming - PowerShell - Shadow Copy - Enable Shadow Copies

Tested on Windows Server 2022.

The below PowerShell code will enable Shadow Copies on the specified drive letter just like it would be enabled if you used VSSUIRUN.exe to enable it.

With one difference: The scheduled task that runs 06:00 and 12:00 now runs every day instead of only monday through friday.

The scheduled task is also created in a way (V1 = legacy job type) that VSSUIRUN.exe can see the job and show that a scheduled shadow copy job is enabled.

# The drive letter to enable Shadow Copies on.
$strDriveLetter = "F"
 
# Delete existing Shadow Storage on the drive.
& "$env:SYSTEMROOT\system32\vssadmin.exe" Delete ShadowStorage /For=$strDriveLetter`: /On=$strDriveLetter`:
 
# Create Shadow Storage on the drive.
& "$env:SYSTEMROOT\system32\vssadmin.exe" Add ShadowStorage /For=$strDriveLetter`: /On=$strDriveLetter`: /MaxSize=10%
 
# Create first Shadow Copy on the drive.
& "$env:SYSTEMROOT\system32\vssadmin.exe" Create Shadow /For=$strDriveLetter`:
 
# Get and store the Volume Path of the driveletter.
$strVolumePath = (Get-Volume -DriveLetter $strDriveLetter).Path
 
# Extract the GUID from the Volume Path of the driveletter.
# By first splitting on "{", and grabbing the second part, then
# splitting the second part on "}", and grabbing the first part.
$strVolumeGUID = $strVolumePath.Split("{")[1].Split("}")[0]
 
# Create 2 daily triggers.                                                                                              
$objTrigger1 = New-ScheduledTaskTrigger -Daily -At 06:00
$objTrigger2 = New-ScheduledTaskTrigger -Daily -At 12:00
 
# Create the action to create a shadow copy.
$objAction = New-ScheduledTaskAction `
               -Execute "$env:SYSTEMROOT\system32\vssadmin.exe" `
               -Argument "Create Shadow /AutoRetry=15 /For=$strVolumePath" 
 
# Create a V1 job and disable default conditions.
# For VSSUIRUN.exe to see the job it needs to be a V1 job (so
# that a .job file is available under %SystemRoot%\Tasks).
$objSettingsSet = New-ScheduledTaskSettingsSet `
  -Compatibility V1 `
  -AllowStartIfOnBatteries:$true `
  -DontStopIfGoingOnBatteries:$true `
  -DontStopOnIdleEnd:$true
 
# Create the scheduled task.
Register-ScheduledTask `
  -TaskName "ShadowCopyVolume{$strVolumeGUID}" `
  -Action $objAction `
  -Trigger $objTrigger1,$objTrigger2 `
  -Settings $objSettingsSet `
  -User "SYSTEM" `
  -Runlevel Highest `
  -Force

See also: The ICT Guy - A quick Server 2016/19 script tutorial on enabling Volume Shadow copy for using Powershell v4/5