$Source = "H:\MSSQL\DBBackups\UserDatabases\Full"
$Dest = "\\NAS00\Backup\"

$ExcludeBackup = Get-ChildItem -recurse $Dest

Get-ChildItem $Source -Recurse -Filter "*.bak" | Copy-Item -Destination $Dest -Exclude $ExcludeBackup

$A=GCI -Path $Source -Filter "*.bak" | select name
$B=GCI -Path $Dest -Filter "*.bak" | select fullname, name

for ($i = 0; $i -lt $B.Count ; $i++) {
  if($B[$i].name -notin $A.name){
    Remove-Item $B[$i].fullname
  }
}
Last modified: September 15, 2020

Author

Comments

Write a Reply or Comment