Search code examples
csvpowershellpowershell-3.0

Powershell for creating folders and subfolders from csv


I try to prepare a script that will create folders and subfolders from CSV.

When I create a CSV with 1 column 'Name' like so

Name
XYZ
ZXX
FFF

with the following script I can achieve creation of folders

$folder = "D:\Test\"
$name = Import-Csv D:\Test\Test.csv
Foreach ($line in $name)
{
    New-Item -path $folder -Name $line.Name -Type Directory
}

What I would like to achieve is to have CSV like this

Name;Letter 
XYZ;A
ZXX;B
FFF;B

I want that the script creates folder $Folder\Letter\Name (so the final path is D:Test\A\XYZ; D:Test\B\ZXX; D:Test\B\FFF). Any advice is welcomed.

Thanks a lot.


Solution

  • This should give you the desired result

    Import-Csv 'D:\Test\Test.csv' -Delimiter ';' |
        % { 
            $path = Join-Path $folder $_.Letter; 
            New-Item -Path $path -Name $_.Name -Type Directory 
        }