'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 1:[1]

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 
    }

Solution 2:[2]

I tried to improve the script little bit. In CSV files is the name and letter that works fine with the script what was provided by DAXaholic, but would need to add some more subfolders into the folder Name. I tried this, but it doesn't work as I need.

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

foreach ($subfolder in $subfolders) 
{
$file | 
%{
 $path2 = Join-Path $folder $_.Letter $_.Name;
 New-Item -Path $path2\$subfolder -Type Directory 
}
    }

What I would like to achieve is following. Script should check the CSV file, create the user in CSV file put it in the folder and create 3 subfolders in the name folder. E.g D:\Test\A\XYZ\Volvo,Skoda,Tesla. What am I missing in the script? Thanks

Solution 3:[3]

if somebody would need similar script, then here is the solution

$folder = "D:\Test\"
$subfolders = "Volvo","Skoda","Tesla"
$file = Import-Csv 'D:\Test\Test.csv' -Delimiter ';' 
$file | 
 % { 
        $path = Join-Path $folder $_.Letter; 
        New-Item -Path $path -Name $_.Name -Type Directory 
}
foreach ($subfolder in $subfolders)
{ 
$file | %{
 $path2 = Join-Path $folder $_.Letter;
 $path3 = Join-Path $path2 $_.Name;
 New-Item -Path $path3\$subfolder -Type Directory 
   }
}

Solution 4:[4]

What I did was put the desired path in my CSV file. Silmiar to the above discussion. "FilePath" can be replaced with any filepath that you have access to read/write from/to. "Folders.csv" is the name of the CSV File that I used. You can name that csv file whatever you want as long as powershell can access it. And that it is something that the computer does not have a problem with the name of the file. Most computers will tell you if it does not like the csv filename that you use. If not PowerShell will tell you.

Here is an example of my PowerShell Code:

$path = "FilePath"
$Name = import-csv -Path 'FilePath\Folders.csv'
foreach ($i in $Name) {
New-Item -Path $path -Name $i.Name -ItemType Directory
}

Sample CSV File has this:

Name;Testing\A\A.1;Testing\B\B.1;Testing\C\C.1;Testing\D\D.1

Note: In the CSV file it would look more like this:

Name
Testing\A\A.1
Testing\B\B.1
Testing\C\C.1
Testing\D\D.1

Solution 5:[5]

I think this is reasonable code:

Set-Location "D:\Folder" 
$subfolders = "1.Requested","2.Evidence","3.Complete"
$Folders = Import-Csv D:\Folder\csv\Fodlers.csv 
 
ForEach ($Folder in $Folders) { 
 
New-Item $Folder.Name -type directory 
  Foreach ($subfolder in $subfolders)
   { 
 $path = Join-Path $Folder.Name -ChildPath " ";
 New-Item $path\$subfolder -Type Directory 
   }
}  

Copyright: Nguyen Ngo Anh Tuan

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 DAXaholic
Solution 2 triskac
Solution 3 triskac
Solution 4 Rajesh Swarnkar
Solution 5