Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I cant figure out what I am doing wrong with this command and would like another set of eyes to point it out.

I want to copy the directory structure but not the files the structure contains.

Copy-Item -recurse -exclude *.* -path \\server1\z$\backups\  -destination \\server2\z$\ -Verbose

The command runs but it copys files despite telling it not to copy files. How do i copy the directory tree and not the files?

share|improve this question
up vote 1 down vote accepted

Try this.

copy-item -path "\\server1\z$\backups\" -Destination "\\server2\z$\" -Filter {$_.PSIsContainer -eq $True} -recurse

share|improve this answer
Thanks, A different method that works. – Jake May 9 '12 at 15:54
BTW, I think the reason your original command didn't work is because the -Include and -Exclude parameters are applied to any wildcards in the -Path parameter. Your original command had no wildcards, so the -Exclude parameter did nothing in your case. – pk. May 9 '12 at 16:23
robocopy /e /XF *.*

Sorry, that didn't really answer your question. Maybe try something like this:

ForEach($folder in Get-ChildItem -Recurse \\server1\z$\backups\ | ? { $_.PSIsContainer }) { Copy-Item -Path $folder -Destination \\server2\z$\$folder }

That above PS script probably needs some tweaking -- I did not really test it. But the concept is there.

share|improve this answer

This is what I came up with. You can take out the "mkdir" command to test before running it.

$src =  "c:\temp"
$dest = "c:\test"

$dirs = dir -Recurse $src | Where {$_.psIsContainer -eq $true}
foreach( $name in $dirs ) {
    $tmp = $name.fullname.Replace($name.Root,$dest + "\")
    mkdir $tmp
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.