Remotely need to delete shadow copies older than 30 days

Hi Admin, i would like to know how to delete shadow copies through remotely which are older than 30 days. I found the script through which i can delete locally but need a script for remote shadow copy deletion. tried to modify the script but not sure whether it works or not, could you please assist.

one or more computer names or IP addresses:

$list = ‘server1’, ‘server2’

authenticate with your current identity:

$result = Get-CimInstance -ClassName Win32_ShadowCopy -ComputerName $list

| ForEach-Object {

$WmiSnapShotDate = $.InstallDate
$strShadowID = $
.ID
$dtmSnapShotDate = [management.managementDateTimeConverter]::ToDateTime($WmiSnapShotDate)
$strClientAccessible = $_.ClientAccessible
$dtmCurDate = Get-Date

$dtmTimeSpan = New-TimeSpan $dtmSnapShotDate $dtmCurDate
$intNumberDays = $dtmTimeSpan.Days

If ($intNumberDays -ge 31 -and $strClientAccessible -eq “True”) {

$_.Delete()
}

}

pratap,
Welcome to the forum. :wave:t4:

When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in PowerShell.org <---- Click :point_up_2:t4: :wink:

Please read the folowing help topics completely to learn how to run command on remote computers

I am trying to delete the shadow copies remotely of each server. But, getting error attached the screenshot.

$list = ‘server1’, ‘server2’

$cred = Get-Credential -Message ‘Authenticate to retrieve WMI information:’
$session = New-CimSession -ComputerName $list -Credential $cred

$result = Get-CimInstance Win32_ShadowCopy -CimSession $session

$result | ForEach-Object {

$WmiSnapShotDate = $.InstallDate
$strShadowID = $
.ID
$strClientAccessible = $_.ClientAccessible
$dtmCurDate = Get-Date
$dtmTimeSpan = New-TimeSpan $WmiSnapShotDate $dtmCurDate
$intNumberDays = $dtmTimeSpan.Days
If ($intNumberDays -ge 31 -and $strClientAccessible -eq “True”) {

$_.Delete()
}

}

Please carefully re-read my first answer.

Follow Olaf’s suggestions.

If you pipe $result through Get-Member, you will see there is no Delete Method as your error suggests. I would think you would need to replace this line with something like this. I have no way to test.

vssadmin delete shadows /Shadow=$Result.ID /Quiet