powershelltreedirectory-structurepowershell-cmdletnew-item

New-Item PowerShell directory tree with multiple directories


I know how to use New-Item in PowerShell so I can create some directories. But can I in anyway make this code shorter?

New-Item 'c:\drivers\Windows\Network\32bit PowerShell' -Type Directory
New-Item 'c:\drivers\Windows\Network\64bit PowerShell' -Type Directory
New-Item 'c:\drivers\Windows\Sound\32bit PowerShell' -Type Directory
New-Item 'c:\drivers\Windows\Sound\64bit PowerShell' -Type Directory
New-Item 'c:\drivers\Utility' -Type Directory
New-Item 'c:\drivers\Software' -Type Directory

As you can see I'm trying to create this directory tree:

C:\drivers
├─Windows
│ ├─Network
│ │ ├─32bit PowerShell
│ │ └─64bit PowerShell
│ └─Sound
│   ├─32bit PowerShell
│   └─64bit PowerShell
├─Utility
└─Software

Solution

  • Make a list of paths and feed it into New-Item:

    $folders = 'C:\drivers\Windows\Network\32bit PowerShell',
               'C:\drivers\Windows\Network\64bit PowerShell',
               ...
    
    $folders | ForEach-Object {
        New-Item -Path $_ -Type Directory
    }