Skip to content

Instantly share code, notes, and snippets.

@Kieranties
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Kieranties/8859485 to your computer and use it in GitHub Desktop.
Save Kieranties/8859485 to your computer and use it in GitHub Desktop.
Copy-ItemWithPath - Copy files while retaining a folder structure
Function Copy-ItemWithPath {
param(
[Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[Alias("FullName")]
[string[]]$item,
[Parameter(Mandatory=$true, Position=1)]
[string]$destination,
[Parameter(Mandatory=$true, Position=2)]
[string]$root
)
begin {
if(-not (Test-Path $destination)){
mkdir $destination | Out-Null
}
}
process {
Push-Location $root
foreach($entry in $item){
$retainPath = (Split-Path (Resolve-Path $item -Relative)) -replace '\.\\(.*)','$1'
$destPath = (Join-Path $destination $retainPath)
mkdir $destPath -Force | Out-Null
Copy-Item $item $destPath
}
Pop-Location
}
}
<#
.EXAMPLE
C:\> Get-ChildItem -Recurse -Include '*.css','*.js','*.html' -File | Copy-ItemWithPath -destination $dest -root $pwd
#>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment