#PSTip Duplicating folder structures

Sometimes you need to create a copy of an exiting directory structure without copying the files, and you also want to include empty folders if they exist. An example would be a deep project directory structure that needs to be created for a new project.

In the days of DOS you could use the XCOPY command. The following command creates the Windows directory structure under the temp directory of drive D:.

XCOPY c:\Windows d:\temp\Windows /E /T /I

In PowerShell you can use the Copy-Item cmdlet. The Filter scriptblock specifies the PSIsContainer property which passes on just folder objects:

Copy-Item $env:windir d:\temp\windows -Filter {PSIsContainer} -Recurse -Force

About the author: Shay Levy

Shay Levy is a Co-founder and editor of the PowerShell Magazine. He is a multiple-year recipient of the Microsoft MVP award, and a Microsoft Certified Trainer (MCT). Shay often covers PowerShell related topics on his blog and you can also follow him on Twitter at @ShayLevy

Related Posts

%d bloggers like this: