title: Recursively copying files matching on pattern
body: <p>I've put together a script that recursively copies from one directory to another, skipping files with a certain pattern in the filename:</p>
  
  <pre>
  function Copy-RevitFiles ([string]$source, [string]$destination, [boolean]$recurse) {
  
      $pattern = '\.\d\d\d\d\.[RVT]'
  
      if ($recurse) {$files = Get-ChildItem $source -Recurse}
      else {$files = Get-ChildItem $source}
  
  
      $files | ForEach-Object {
      if ((Select-String -InputObject $_.Name -pattern $pattern -AllMatches -quiet) -eq $null) {
          #Write-Host $_.Name
          #Write-Host $_.Fullname
          #Write-Host "$($destination)\$($_.FullName.TrimStart($source))"
          Copy-Item $_.FullName -Destination "$($destination)\$($_.FullName.TrimStart($source))" #add on full name of item, less $source start end of file path
          #Write-Host "----------"
          }
      }
  }
  </pre>
  
  <p>It works well, for the most part.  The problem I have though is that it creates an additional subfolder inside each folder with files in it.  For example:</p>
  
  <p>If input the source as a directory with this structure:</p>
  
  <pre>
  Source
  -file1.rvt
  -file1.0225.rvt (will not copy as it matches the pattern)
  -file1.0226.rvt (will not copy as it matches the pattern)
  -folder1
   |-file2.rvt
   |-file2.0121.rvt (will not copy as it matches the pattern)
   |-file2.0122.rvt (will not copy as it matches the pattern)
  -folder2
  </pre>
  
  <p>I am expecting the following structure to be created in the destination folder:</p>
  
  <pre>
  Destination
  -file1.rvt
  -folder1
   |-file2.rvt
  -folder2
  </pre>
  
  <p>But instead, I am getting:</p>
  
  <pre>
  Destination
  -file1.rvt
  -folder1
   |-file2.rvt
   |-folder1 (extra folder not in source)
  -folder2
  </pre>
  
  <p>Any idea where I am going wrong?</p>