PowerShell How-To

How To Maintain Folder Structure When Copying Files with PowerShell

Transferring files with multiple subfolders gets a bit tricky. Here's how to avoid any issues.

Copying files in PowerShell is easy. Copy-Item is your friend. Simply specify the source and destination and you're done. The same even goes for entire folders. It's easy to copy entire folders or all files in a folder. The problem arises when you get picky; when you want to copy only some files out of lots of different folders at once and copy them somewhere else.

What do I mean by this?  Let me give you an example.  Let's say that I have three folders (in reality this may be many more) that contain files as well as other subfolders and those subfolders contain files and subfolders, etc. You have a structure that looks something like this:

FOLDER1

  1. textfile1.txt
  2. textfile2.txt
  3. SUBFOLDER1
    1. otherfile.mdb
    2. somethingelse.xls
    3. textfile3.txt

FOLDER2

  1. textfile4.txt
  2. SUBFOLDER2
    1. othddderfile.mdb
    2. somethdfdfdingelse.xls
    3. textfile3.txt
    4. SUBFOLDER3
      1. anothertextfile.txt
      2. yetanoter.txt
      3. spreadsheet.xlsx

FOLDER3

  1. textfile4.txt
  2. SUBFOLDER4
    1. othddderfile.mdb
    2. somethdfdfdingelse.xls
    3. textfile3.txt
    4. SUBFOLDER5
      1. anothertextfile.txt
      2. yetanoter.txt
      3. spreadsheet.xlsx
  3. SUBFOLDER6
    1. adfdfdf.txt
    2. ttttt.txt
    3. hello.bat
      1. SUBFOLDER7
        1. ljljklkj.txt

You can see I have lots of folders and files in folders, etc. I'm doing a file server migration but I only want to migrate all of the text files. The text files are in important folder locations so I don't just want to copy the files over and throw them in a single folder. I want to maintain the same folder structure as on the other server.

Let's say I just want to copy all of the text files to another folder on the same server just for an example.  I'll copy all the text files to C:\DestinationFolder. First, let's ensure I can find all the text file first.

Get-ChildItem .\folder1,.\folder2,.\folder3 -Recurse *.txt

 

This works. It outputs all of the text files anywhere in either of my three folders.  Now, I'll copy them all over to C:\DestinationFolder.

[Click on image for larger view.]  Figure 1.

See the problem?  I lost the folder structure. I don't know where they came from in the source. I want to maintain that folder structure. How do I do that?  With a little ingenuity!

## Find the source files
Get-ChildItem .\folder1,.\folder2,.\folder3 -Recurse *.txt | foreach {
## Remove the original root folder
$split = $_.Fullname -split '\\'
$DestFile = $split[1..($split.Length - 1)] -join '\'
## Build the new destination file path
$DestFile = "C:\DestinationFolder\$DestFile"
## Copy-Item won't create the folder structure so we have to
## create a blank file and then overwrite it
$null = New-Item -Path $DestFile -Type File -Force
Copy-Item -Path $_.FullName -Destination $DestFile -Force
}

In a nutshell, here's the breakdown of what this little code snippet is doing.

  1. Find all the text files in all source folders.
  2. Replace the C:\ reference in the source directory name of each of the text files with C:\DestinationFolder.
  3. Create a "filler" file with New-Item -Force because Copy-Item will not create the folder structure.
  4. Copy the source file to the destination file path.

Using this technique you can copy specific files from 2, 5, 5,000 folders from one folder, create the folder structure in another folder or another server and copy those files in the same folder that they came from.

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


comments powered by Disqus
Most   Popular

SharePoint Watch

Sign up for our newsletter.

Terms and Privacy Policy consent

I agree to this site's Privacy Policy.