#PSTip Create an empty folder structure

Note: This tip requires PowerShell 2.0 or above.

In an earlier tip, you saw how an empty file of a specified size can be created. In today’s tip, we shall see how an empty folder structure can be created.

In the good old DOS days, we would create an empty folder structure by using md or mkdir commands. This is the PowerShell era. So, how do we do that in PowerShell? Simple:

PS> New-Item -ItemType Directory -Path ".\FolderX\FolderY\FolderZ"

That is it!

The ‘.’ at the beginning of -Path parameter value tells New-Item cmdlet to create the folder structure in the present working directory. Without this, the folder structure gets created at the root of the current drive.

But wait, did you know that we have md and mkdir commands in PowerShell too? mkdir is a function defined in PowerShell that uses New-Item cmdlet to create folder(s) and md is an alias to mkdir.

PS C:\> Get-Command mkdir

CommandType    Name    ModuleName
-----------    ----    ----------
Function       mkdir

PS C:\> Get-Alias md

CommandType    Name        ModuleName
-----------    ----        ----------
Alias          md -> mkdir

So, now, we can use these commands the same way we used New-Item cmdlet.

PS> md ".\FolderX\FolderY\FolderZ"

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts

%d bloggers like this: