Skip to content

Instantly share code, notes, and snippets.

@Himura2la
Created August 3, 2020 09:44
Show Gist options
  • Save Himura2la/cdbb1488cbc2250d49d1213ccac3ec47 to your computer and use it in GitHub Desktop.
Save Himura2la/cdbb1488cbc2250d49d1213ccac3ec47 to your computer and use it in GitHub Desktop.
Copy-Item -Recurse with multiple filter
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