How to use PowerShell copy-item and keep structure

copy-item container
powershell copy folder structure and files exclude
copy files preserve directory structure windows
powershell copy directory structure only
powershell copy-item exclude folder
powershell copy-item exclude hidden
powershell copy-item include hidden files
xcopy keep folder structure

I have a directory structure that looks like this:

C:\folderA\folderB\folderC\client1\f1\files
C:\folderA\folderB\folderC\client1\f2\files
C:\folderA\folderB\folderC\client2\f1\files
C:\folderA\folderB\folderC\client2\f2\files
C:\folderA\folderB\folderC\client3\f1\files
C:\folderA\folderB\folderC\client4\f2\files

I want to copy the content of the f1 folders in C:\tmp\ to get this

C:\tmp\client1\f1\files
C:\tmp\client2\f1\files
C:\tmp\client3\f1\files

I tried this:

Copy-Item -recur -path: "*/f1/" -destination: C:\tmp\

But it copies the contents without copying the structure correctly.

In PowerShell version 3.0 and newer this is simply done this way:

Get-ChildItem -Path $sourceDir | Copy-Item -Destination $targetDir -Recurse -Container

Reference: Get-ChildItem

PowerTip: Use PowerShell to Copy Items and Retain Folder Structure, PowerTip: Use PowerShell to Copy Items and Retain Folder Structure. Doctor Scripto. Dr Scripto. July 4th, 2013. Summary: Copy items in a folder and retain the​  How can I use Windows PowerShell 3.0 to copy a folder structure from a drive to a network share, and retain the original structure? Use the Copy-Item cmdlet and specify the –Container switched parameter: Copy-Item c:\fso –destination \\server1\share -recurse -container

Use xcopy or robocopy, both of which have been designed for exactly that purpose. Assuming your paths are only filesystem paths, of course.

Copy, Move and Rename Files Using Windows PowerShell, , and after the –destination parameter, type the path of the destination folder. I also want you to be aware that I am using PowerShell version 5.0 here, so it did not cause any issues. The actual task was to be done on a Windows Server 2008 R2 and the PowerShell version that I was left with was 2.0. Now Copy-Item has some limitations when it comes to this version as it cannot create the Folder structure on the destination.

How to copy multiple files in PowerShell using Copy-Item?, How do I copy a folder and content in PowerShell? The Copy-Item cmdlet copies an item from one location to another location in the same namespace. For instance, it can copy a file to a folder, but it can't copy a file to a certificate drive. This cmdlet doesn't cut or delete the items being copied. The particular items that the cmdlet can copy depend on the PowerShell provider that exposes the item.

I have been digging around and found a lot of solutions to this issue, all being some alteration, not just a straight copy-item command. Granted, some of these questions predate PowerShell 3.0 so the answers are not wrong, but using PowerShell 3.0 I was finally able to accomplish this using the -Container switch for Copy-Item:

Copy-Item $from $to -Recurse -Container

This was the test I ran, no errors and the destination folder represented the same folder structure:

New-Item -ItemType dir -Name test_copy
New-Item -ItemType dir -Name test_copy\folder1
New-Item -ItemType file -Name test_copy\folder1\test.txt

# NOTE: with no \ at the end of the destination, the file
#       is created in the root of the destination, and
#       it does not create the folder1 container
#Copy-Item D:\tmp\test_copy\* D:\tmp\test_copy2 -Recurse -Container

# If the destination does not exist, this created the
# matching folder structure and file with no errors
Copy-Item D:\tmp\test_copy\* D:\tmp\test_copy2\ -Recurse -Container

How to use PowerShell commands to copy files and folders, Find all the text files in all source folders. Replace the C:\ reference in the source directory name of each of the text files with C:\DestinationFolder. Create a "filler" file with New-Item -Force because Copy-Item will not create the folder structure. Copy the source file to the destination file path. Let’s suppose you have a folder structure which you need to copy to another place and exclude some subfolders and files during copying. For an instance, you have: And you need to copy the content of…

If you want to correctly copy a folder structure correctly with PowerShell, do it like so:

$sourceDir = 'C:\source_directory'
$targetDir = 'C:\target_directory'

Get-ChildItem $sourceDir -Recurse | % {
   $dest = $targetDir + $_.FullName.SubString($sourceDir.Length)

   If (!($dest.Contains('.')) -and !(Test-Path $dest))
   {
        mkdir $dest
   }

   Copy-Item $_.FullName -Destination $dest -Force
}

This accounts for creating directories and just copying the files. Of course you'll need to modify the Contains() call above if your folders contain periods or add a filter if you want to search for "f1" as you mentioned.

How To Maintain Folder Structure When Copying Files with , I cannot see a parameter on copy-item to preserve folder structure from the to have a limitation and using get-childitem in combination with copy-item has a  By using Copy-Item, PowerShell allows a developer to copy files and folders a number of different ways. Basic Usage. At it's most basic, the Copy-Item cmdlet copies a single file from point A to point B using the Path parameter as the source file path and the Destination parameter as the destination folder path.

Copy-Item does not preserve directory structure, In the above example,I was first listing the files and folders recursively and selecting only the files which ended with .log extension. Then I was  Copy Files from Multiple Directories and Merge into One Folder Copy-Item C:\Source\A\*, C:\Source\B\* C:\Destination This would create a flat folder structure by copying all files from directory “A” and “B” and paste the files into “Destination” folder. 4.

PowerShell to Copy Items and Retain Folder Structure, Copying files in PowerShell can be a snap if you know how to use the Copy-Item command correctly. Learn all about this useful command in  I want the source path 'folders' to be created and the files placed in them accordingly (no matter how deep the folder structure goes) I cannot see a parameter on copy-item to preserve folder structure from the source when copying. is there a way around this or is thing something Microsoft are going to add (I am using PowerShell 5.1) Thank you

Copy-Item: Copying Files like a Boss in PowerShell, Exclude option doesn't work recursively. It's only checked for items which being copied exactly by Copy-Item cmdlet. In our sample they are  This can be done just using Copy-Item. No need to use Get-Childitem. I think you are just overthinking it. Copy-Item -Path C:\MyFolder -Destination \\Server\MyFolder -recurse -Force I just tested it and it worked for me. edit: included suggestion from the comments

Comments
  • What does the -container flag do?
  • @Cullub Regarding the -container flag, see What is the meaning of Powershell's Copy-Item's -container argument?
  • Note that in this example, $targetDir must already exist. In my testing I found that when $targetDir does not exist, the files will be copied but the directory structure will be flattened.
  • bwerks is correct. No idea why this received any votes.
  • this is not working, i just tryed and the structure is just flattened as said in bwerks comment
  • I know but I'd like to do that in powershell because that's a part of a bigger script and I'm going to pipe the output of copy (using -PassThru) to other commands.
  • you can use cmd /c xcopy to use xcopy from powershell
  • @ZacharyYates: You can use xcopy to use xcopy from PowerShell. It's not a cmd built-in, so there is no need to use cmd /c here.
  • @joey Didn't know that - awesome!
  • few answers below speak to a -container switch, worth taking a look at. copy-item -container will do just that without all the extra work native to powershell.
  • I don't understand, is this powershell? Or a .bat script? Sorry I am new to NT
  • FWIW, this fails if you put a trailing directory separator on $sourceDir.
  • Works for PS 2.0 and PS 4.0 ? Mabye any differences in cmdlets.
  • The code above tries to create files of every folder. A fast fix would be to exclude the folders like so: foreach{ if( $_.Attributes -neq 'Directory' ){ $targetFile =... } }