I got the following problem. I have a folder C:\Users\Username. In that folder you can find folders like "Documents", "Favorites" etc.

What im trying to do is including only some of the folder to copy.

I was playing arround with Robocopy XD to exclude some folder but that is not acceptable cuz i cant know all the folder that may be there.

Then i tried Get-ChildItem -include <some dir>. Now i want to copy only the listed folders. But robocopy copies only the content of the folder and not the folder itself. Are there any other solutions for this problem.

2 Answers

3
Iftimie Tudor On

First : don't forget to use -recurse on Get-ChildItem to grab all the structure in the object.
Then on robocopy:

  • You need /s to include non-empty subdirectories and /e to include the all the subdirs
  • /b and /purge is nice when you are doing backups - greatly increased speed . be careful with purge though
  • /xd <Directory>[ ...] Excludes directories that match the specified names and paths. (or you can include just a list of the desired subdirs)

I use something like this:

$source = 'C:\hold\first test'
$destination = 'C:\hold\second test'
$robocopyOptions = @('/NJH', '/NJS')
$fileList = 'test.txt'
Start robocopy -args "$source $destination $fileList $robocopyOptions"

Also the log option saved my life a couple of times.

For all the parameters see robocopy docs

0
seyo -IV On

I have just found out you can do Copy-Item -Path $path"\Desktop" -Destination $newdir -Recurse and it will copy the Desktop folder with its content.