title: Powershell Exclusion of Folders with matching filenames
body: <p>I'm using something like the below script to exclude Folders.
  The problem is that there is a file name as of a folder name and i only want to exclude the folder and not the file. For example in the below i want to exclude "B" Folder only and not "B.txt" file whereas the current code as shown below excludes both file and folder.</p>
  
  <pre><code>$exclude = @("*.cer")
  $excludeMatch = @("Member", "A", "B" , "C" , "D")
  [regex] $excludeMatchRegEx = ‘(?i)‘ + (($excludeMatch |foreach {[regex]::escape($_)}) –join “|”) + ‘’
  Get-ChildItem -Path $source -Recurse -Exclude $exclude | 
  where { $excludeMatch -eq $null -or `enter code here`$_.FullName.Replace($Source, "") -notmatch $excludeMatchRegEx} |
   Copy-Item -Destination {
   if ($_.PSIsContainer) {
     Join-Path $Dest `enter code here`$_.Parent.FullName.Substring($source.length)
    } else {
     Join-Path $Dest $_.FullName.Substring($source.length)
   }
   } -Force -Exclude $exclude 
  </code></pre>