none
Create folder and sub-folders using a "Path", in Powershell RRS feed

  • Question

  • Hi All,

    Is there a way to create Folders and sub-folders, using PATH, in PowerShell?

    new-item -type directory -path C:\Folder1\Folder2\Folder3

    I am willing to create folder structure as "C --> Folder1 --> Folder2--> Folder3". However, if Folder2 is already existing, then I wish not to re-create it, as it would throw an expected error.

    While trying, I end up creating only Folder3, in C drive.

    How should I improvise my code here?

    Many thanks in advance.

    Abhi..

    Thursday, July 24, 2014 6:11 AM

Answers

  • PowerShell V3 or later:

    new-item -type directory -path C:\Folder1\Folder2\Folder3 -Force

    It will not recreate an existing folder and does not create errors. You can run it over and over again and it produces no errora.

    Try it.


    ¯\_(ツ)_/¯

    • Proposed as answer by jrv Thursday, July 24, 2014 10:18 AM
    • Marked as answer by ScriptingWifeModerator Tuesday, September 16, 2014 7:22 PM
    Thursday, July 24, 2014 10:18 AM