1

#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: ,

One Response to "#PSTip Create an empty folder structure"

  1. ranya says:

    Hi Ravikanth

    I hardly search an answer to my need but non succes has been found. Maybe you can help me out.
    here is my history:

    I want to create a herarichal tree of folders using a script in powershell.
    here is the design of the tree:

    Folder root
    Folder_1
    Folder_1.1
    File
    Folder_1.2
    File
    Folder_2

    How can I do?

Leave a Reply

Submit Comment

© 2016 PowerShell Magazine. All rights reserved. XHTML / CSS Valid.
Proudly designed by Theme Junkie.
%d bloggers like this: