Skip to content

Instantly share code, notes, and snippets.

@FlorianHeigl
Last active May 10, 2023 16:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FlorianHeigl/da66e02edab336a5e82599383769b35a to your computer and use it in GitHub Desktop.
Save FlorianHeigl/da66e02edab336a5e82599383769b35a to your computer and use it in GitHub Desktop.
powershell delete vss snapshots via wmi
# source: https://github.com/MSAdministrator/PoshCodeMarkDown/blob/master/docs/Delete-Old-Shadow-Copies.ps1.md
# modified...
# TODO: only handle c:
# TODO: Never delete the last snapshot, or even better the last two?
Get-WmiObject Win32_Shadowcopy | ForEach-Object {
.InstallDate $strShadowID = $.ID $dtmSnapShotDate = [management.managementDateTimeConverter]::ToDateTime($WmiSnapShotDate) $strClientAccessible = $_.ClientAccessible $dtmCurDate = Get-Date
$dtmTimeSpan = New-TimeSpan $dtmSnapShotDate $dtmCurDate $intNumberDays = $dtmTimeSpan.Days
If ($intNumberDays -ge 7 -and $strClientAccessible -eq "True") {
$_.Delete() }
}
@FlorianHeigl
Copy link
Author

FlorianHeigl commented May 10, 2023

https://serverfault.com/a/527384/106483 looks better, especially

$shadowCopies[0].Delete() - Deletes the first shadow copy when more than one exists

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment