Powershell script to copy changed files from one folder to another folder with same folder structure

Powershell script to copy changed files from one folder to another folder with same folder structure.

Below script help you to copy changed files since last 90 days to a new folder with same folder structure

$SourceFolder =  "C:\Source"
$destinationfolder = "d:\dest"
$days="90"

try {
Get-ChildItem $SourceFolder -Recurse  | Where-Object {$_.LastWriteTime -le (Get-Date).AddDays(-$days)} | ForEach-Object {
    $PathArray = $_.FullName.Replace($SourceFolder,"").ToString().Split('\') 

    $Folder = $destinationfolder

    for ($i=1; $i -lt $PathArray.length-1; $i++) {
        $Folder += "\" + $PathArray[$i]
        if (!(Test-Path $Folder)) {
            New-Item -ItemType directory -Path $Folder
        }
    }   
    $NewPath = Join-Path $destinationfolder $_.FullName.Replace($SourceFolder,"")


   Copy-Item $_.FullName -Destination $NewPath
   Write-Host "Copying $_ " -ForegroundColor Green

}
}
catch
{
write-host $_ -ForegroundColor Red
}

Comments