1
votes

This seems like a simple operation but I can't figure out how to get Powershell to copy an entire folder structure from one location to another but exclude one folder (called 'connections') and its contents.

I've tried combining Copy-Item and Get-ChildItem like so

cpi (gci folder1 -Exclude connections) folder2  -recurse

but it seems the -recurse parameter overwrites the -exclude parameter and the connections folder and its contents are copied. Without -recurse the contents of the folders I do want copied are ignored.

1

1 Answers

2
votes

I'm not sure why that isn't working, it seems to behave correctly on my machine.

You could always pipe to Copy-Item:

Get-ChildItem folder1 | where { !(($_ -is [System.IO.DirectoryInfo]) -and ($_.Name -eq "connections")) } | Copy-Item -Destination folder2 -Recurse

The advantage of this is that you can just get PowerShell to print out the output after:

Get-ChildItem folder1 | where { !(($_ -is [System.IO.DirectoryInfo]) -and ($_.Name -eq "connections")) }

That way you can check exactly what is getting copied (i.e. is the "connections" folder missing?)