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.
- We will create 5 folders inside it as shown in the image.
- Inside “My Folder 1” we will create 5 more folders.
- Now we create a new folder called secondteckangaroo in the same path as teckangaroo.
- The secondteckangaroo is empty.
- We will run the following command to replicate the folder trees of teckangaroo into secondteckangaroo.
- We check the folder secondteckangaroo and see that the first level folders are created properly.
- Interestingly the date of modified is the same for all the folders.
- 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“.
We have successfully achieved the folder tree replication using PowerShell.
Now let us break down the script one by one.
If we enter the following into PowerShell we will see only the folder trees of the current directory.
- We will enter that in the teckangaroo folder and check.
- Note that we are passing the full path of the teckangaroo folder in the first half of the script.
- 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.
- 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.
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