Exclude list in PowerShell Copy-Item does not appear to be working

I think the best way is to use Get-ChildItem and pipe in the Copy-Item command.

I found that this worked:

$source="d:\t1"
$dest="d:\t2"
$exclude = @('*.pdb','*.config')
Get-ChildItem $source -Recurse -Exclude $exclude | Copy-Item -Destination {Join-Path $dest $_.FullName.Substring($source.length)}

Basically, what is happening here is that you’re going through the valid files one by one, then copying them to the new path. The ‘Join-Path’ statement at the end is so that the directories are also kept when copying over the files. That part takes the destination directory and joins it with the directory after the source path.

I got the idea from here, and then modified it a bit to make it work for this example.

I hope it works!

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)