Skip to content

Instantly share code, notes, and snippets.

@winkler-winsen
Created April 26, 2024 09:28
Show Gist options
  • Save winkler-winsen/7f34bbf76a4717290cee5303bd623da7 to your computer and use it in GitHub Desktop.
Save winkler-winsen/7f34bbf76a4717290cee5303bd623da7 to your computer and use it in GitHub Desktop.
PowerShell copy files with filter and maintain directory structure
$sourceDir = 'C:\SourceDir\'
$targetDir = 'C:\DestDir\'
Get-ChildItem $sourceDir -Filter "*" -Recurse |`
Where-Object {$_.LastWriteTime -gt (Get-Date).addDays(-1) -and -not $_.PSIsContainer} |`
foreach {
$targetFile = $targetDir + $_.FullName.SubString($sourceDir.Length);
New-Item -ItemType File -Path $targetFile -Force;
Copy-Item $_.FullName -destination $targetFile
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment