#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"
Filed in: Columns, Tips and Tricks Tags: ,
© 6407 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: