in

WindowsWindows Loving ItLoving It

How to replicate folder trees using PowerShell [2020 Latest]

How to replicate folder trees using PowerShell using a single script. This script will replicate the folder structure without copying files etc.

replicate folder trees

In this post, we will show you how to replicate the folder trees of one folder to another folder.

  • For this purpose, we will create a folder called teckangaroo as shown in the image.
teckangaroo folder
teckangaroo folder
  • We will create 5 folders inside it as shown in the image.
5 folders inside teckangaroo
5 folders inside teckangaroo
  • Inside “My Folder 1” we will create 5 more folders.
child directories of teckangaroo\my folder 1
child directories of teckangaroo\my folder 1
  • Now we create a new folder called secondteckangaroo in the same path as teckangaroo.
secondteckangaroo folder
secondteckangaroo folder
  • The secondteckangaroo is empty.
contents of secondteckangaroo
contents of secondteckangaroo
  • We will run the following command to replicate the folder trees of teckangaroo into secondteckangaroo.
Get-ChildItem ‘e:\teckangaroo’ -Name -attributes D -Recurse | foreach{md $_}
  • We check the folder secondteckangaroo and see that the first level folders are created properly.
contents of secondteckangaroo
contents of secondteckangaroo
  • Interestingly the date of modified is the same for all the folders.
time modified
time modified
  • We check the “My Folder 1” in secondteckangaroo.
  • The second level folder structure is the replica of the second level folder structure from “teckangaroo\My Folder 1“.
secondteckangaroo\my folder 1
secondteckangaroo\my folder 1

We have successfully achieved the folder tree replication using PowerShell.

Now let us break down the script one by one.

Get-ChildItem ‘e:\teckangaroo’ -Name -attributes D -Recurse | foreach{md $_}

If we enter the following into PowerShell we will see only the folder trees of the current directory.

Get-ChildItem ‘e:\teckangaroo’ -Name -attributes D -Recurse
  • We will enter that in the teckangaroo folder and check.
check child directories
check child directories
  • Note that we are passing the full path of the teckangaroo folder in the first half of the script.
‘e:\teckangaroo’
  • The -Recurse flag is need so that the script does not stop at the first folder level and continues to dig into the child folders also.
  • We need to provide the -attributes D flag (-attributes) and value (D) to ensure that the script does not give file names along with directory names.
  • The | is a pipeline operator. It is used to take the result of the preceding script and feed it to the script following the | (pipeline operator).
  • The second script is a foreach PowerShell script where the $_ is used to get the individual item in each iteration.  In other words, the $_ is a placeholder variable.

foreach{md $_}

  • md is used for making directory.
  • Therefore, the second script makes directory in an iterative manner.  And the list is the output of the first script.
  • The output of the first script is the folder tress of that folder.
  • Thus both scripts combine together to replicate the folder trees of one folder in another.

Conclusion:

In this post, we learned how to replicate the folder trees of one folder in another using a simple PowerShell script.  We can replace the input file path to whatever path we want and replicate folder structures and trees with ease.

For more awesome posts on PowerShell please stay tuned to TecKangaroo.

PowerShell replicate folder trees
  • Article Rating
  • Content Relevance
  • Content Clarity
  • Author Rating
4.6

Leave a Reply

Your email address will not be published. Required fields are marked *

Loading…

0

Comments

0 comments