Powershell: Copy with Folder Structure intact / Clear Backup older then specified day's

 

 

Script 1. Copy Dir to Share with Folder Structure intact.

$sourceDir = 'D:\BACKUP\' $targetDir = '\\IP\backup\'

Get-ChildItem $sourceDir -Recurse | % {    $dest = $targetDir + $_.FullName.SubString($sourceDir.Length)

   If (!($dest.Contains('.')) -and !(Test-Path $dest))    {         mkdir $dest    }    if ($_.CreationTime -gt ($(Get-Date).AddDays(-5)))    {    Copy-Item $_.FullName -Destination $dest -Force    } }

 

Script 2. Clear backup retention with a period of over 19 day's old.

# set folder path
$dump_path = "D:\BACKUP\"
 
# set max age of files
$max_days = "-19"
 
# get the current date
$curr_date = Get-Date
 
# determine how far back we go based on current date
$del_date = $curr_date.AddDays($max_days)
 
# delete the files
Get-ChildItem $dump_path -Recurse | Where-Object { $_.CreationTime -lt $del_date } | Remove-Item -Recurse -Force -Confirm:$False

 

Script 3. Delete items of network share older then 26 day's.

# set folder path
$dump_path = "\\IP\backup\PST"
 
# set min age of files
$max_days = "-26"
 
# get the current date
$curr_date = Get-Date
 
# determine how far back we go based on current date
$del_date = $curr_date.AddDays($max_days)
 
# delete the files
Get-ChildItem $dump_path -Recurse | Where-Object { $_.CreationTime -lt $del_date } | Remove-Item -Recurse -Force -Confirm:$False