Thursday, April 9, 2009

Powershell Copy-Item does not honor the exclude parameter

I've been having a hard time getting Powershell's Copy-Item to work for me. My problem is that the -exclude parameter for the Copy-Item only honors the excluded items for the root directory in the destination but not for sub-directories if you also include the recurse parameter.

I believe that I have finally solved the problem with this snippet of powershell:

$source = 'd:\t1'

$dest = 'd:\t2'

$exclude = @('*.pdb','*.config')

$items = Get-ChildItem $source -Recurse -Exclude $exclude

foreach($item in $items)

{

      $target = Join-Path $dest $item.FullName.Substring($source.length)

      if( -not( $item.PSIsContainer -and (Test-Path($target))))

      {

            Copy-Item -Path $item.FullName -Destination $target

      }

}

1 comment: