Created
August 3, 2020 09:44
-
-
Save Himura2la/cdbb1488cbc2250d49d1213ccac3ec47 to your computer and use it in GitHub Desktop.
Copy-Item -Recurse with multiple filter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Copy-Filtered { | |
param ( | |
[string] $Source, | |
[string] $Target, | |
[string[]] $Filter | |
) | |
$ResolvedSource = Resolve-Path $Source | |
$NormalizedSource = $ResolvedSource.Path.TrimEnd([IO.Path]::DirectorySeparatorChar) + [IO.Path]::DirectorySeparatorChar | |
Get-ChildItem $Source -Include $Filter -Recurse | ForEach-Object { | |
$RelativeItemSource = $_.FullName.Replace($NormalizedSource, '') | |
$ItemTarget = Join-Path $Target $RelativeItemSource | |
$ItemTargetDir = Split-Path $ItemTarget | |
if (!(Test-Path $ItemTargetDir)) { | |
[void](New-Item $ItemTargetDir -Type Directory) | |
} | |
Copy-Item $_.FullName $ItemTarget | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment