Create Folder Structure from CSV using PowerShell

Yesterday, one of my clients wanted us to create a complex folder structure for organizing projects and related files. The entire folder structure is presented in a .csv file.

In this tutorial, I’ll walk you through the steps to create a PowerShell script that generates a folder structure based on data from a CSV file.

CSV File

The first step is to create a CSV file that contains the information for your desired folder structure. The CSV file should have columns representing the hierarchy of folders and subfolders. For example:

Parent Folder,Subfolder 1,Subfolder 2
Projects,Client A,Project 1
Projects,Client A,Project 2
Projects,Client B,Project 3
Archive,2025,January
Archive,2025,February

Save this file with a descriptive name, such as “FolderStructure.csv,” in an easily accessible location.

You can also download this file and use it for practice.

Now, let me show you how to create the folder structure using this CSV file in PowerShell.

I saved this file in the Bijay folder in the C drive.

Method 1: Using a ForEach Loop

One way to create the folder structure from the CSV file is by using a ForEach loop in PowerShell. Here’s an example script:

$csvFile = "C:\Bijay\FolderStructure.csv"
$rootFolder = "C:\Bijay\MyFolderStructure"

$csv = Import-Csv $csvFile

foreach ($row in $csv) {
    $parentFolder = Join-Path $rootFolder $row.'Parent Folder'
    $subfolder1 = Join-Path $parentFolder $row.'Subfolder 1'
    $subfolder2 = Join-Path $subfolder1 $row.'Subfolder 2'

    New-Item -ItemType Directory -Path $subfolder2 -Force
}

Let’s break down the script:

  1. We specify the path to the CSV file and the root folder where the structure will be created.
  2. We import the CSV file using the Import-Csv cmdlet and store it in the $csv variable.
  3. We loop through each row of the CSV using a ForEach loop.
  4. For each row, we construct the full path for the parent folder, subfolder 1, and subfolder 2 using the Join-Path cmdlet.
  5. Finally, we create the folders using the New-Item cmdlet with the -ItemType Directory parameter and the -Force switch to overwrite existing folders if necessary.

Save this script with a “.ps1” extension, such as “CreateFolderStructure.ps1”.

I executed the above PowerShell script using VS code, and you can see the exact output in the screenshot below:

Create Folder Structure from CSV using PowerShell

Method 2: Using the mkdir Function

Another approach is to use the built-in mkdir function in PowerShell to create the folders directly from a CSV file. Here’s an example script:

$csvFile = "C:\Bijay\FolderStructure.csv"
$rootFolder = "C:\Bijay\MyFolderStructure"

$csv = Import-Csv $csvFile

foreach ($row in $csv) {
    $folderPath = Join-Path $rootFolder ($row.'Parent Folder' + "\" + $row.'Subfolder 1' + "\" + $row.'Subfolder 2')
    mkdir $folderPath -Force
}

In this script, we concatenate the folder names from each row of the CSV to create the full folder path. Then, we use the mkdir function to create the folders directly, with the -Force switch to overwrite existing folders if needed.

If you have saved this as a .ps1 file, then to run the PowerShell script, follow these steps:

  1. Open PowerShell as an administrator.
  2. Navigate to the directory where you saved the script using the cd command. For example:
   cd C:\Scripts
  1. Run the script by typing its name and pressing Enter. For example:
   .\CreateFolderStructure.ps1

PowerShell will execute the script and create the folder structure based on the data in the CSV file.

Best Practices

Here are some best practices and tips to keep in mind when creating folder structures using PowerShell:

  • Always test your script on a small sample CSV file before running it on a large dataset to ensure it works as expected.
  • Use meaningful names for your CSV columns and folders to make the structure easy to understand.
  • Be cautious when using the -Force switch, as it will overwrite existing folders without prompting for confirmation.
  • Consider adding error handling and logging to your script to track any issues that may occur during execution.
  • If your CSV file has a different delimiter than a comma, you can specify it using the -Delimiter parameter of the Import-Csv cmdlet.

Check out Remove Blank Lines from CSV Files Using PowerShell

Create Folder Structure from CSV – Real Example

I will see a real example of creating a folder structure from a CSV file here.

Project Organization

Suppose you’re a project manager working for a software development company in San Francisco. You have multiple clients, each with several projects.

To keep everything organized, you create a CSV file with the following columns: “Client”, “Project”, “Year”. You can then use the PowerShell script to generate a folder structure like this:

Here is the CSV file that you can also use.

Client,Project,Year
Client A,Project 1,2025
Client A,Project 1,2026
Client A,Project 2,2025
Client A,Project 2,2026
Client B,Project 3,2025
Client B,Project 3,2026
Client B,Project 4,2025
Client B,Project 4,2026

You can use the below PowerShell script to create the folder structure from the above CSV file.

$csvFile = "C:\Bijay\ProjectStructure.csv"
$rootFolder = "C:\Bijay\Projects"

$csv = Import-Csv $csvFile

foreach ($row in $csv) {
    $clientFolder = Join-Path $rootFolder $row.Client
    $projectFolder = Join-Path $clientFolder $row.Project
    $yearFolder = Join-Path $projectFolder $row.Year

    New-Item -ItemType Directory -Path $yearFolder -Force
}

Let me now show you another real example that I did for another client.

Check out PowerShell Export-CSV cmdlet

File Archiving

Let’s say you need to archive client files based on the year and month they were closed. You create a CSV file with the columns: “Client Name”, “Year”, “Month”.

Here is the CSV file:

Client Name,Year,Month
Client A,2025,January
Client A,2025,February
Client A,2025,March
Client A,2026,January
Client A,2026,February
Client B,2025,April
Client B,2025,May
Client B,2026,January
Client B,2026,February

The ideal folder structure will look like below:

   Archive/
   ├── Client A/
   │   ├── 2025/
   │   │   ├── January/
   │   │   ├── February/
   │   │   └── March/
   │   └── 2026/
   │       ├── January/
   │       └── February/
   └── Client B/
       ├── 2025/
       │   ├── April/
       │   └── May/
       └── 2026/
           ├── January/
           └── February/

To make this folder structure from the above CSV file using PowerShell, you can write the below script.

$csvFile = "C:\Emily\ArchiveStructure.csv"
$rootFolder = "C:\Emily\Archive"

$csv = Import-Csv $csvFile

foreach ($row in $csv) {
    $clientFolder = Join-Path $rootFolder $row.'Client Name'
    $yearFolder = Join-Path $clientFolder $row.Year
    $monthFolder = Join-Path $yearFolder $row.Month

    New-Item -ItemType Directory -Path $monthFolder -Force
}

Conclusion

In this tutorial, I have explained how to create folder structures from CSV files using PowerShell. I have also shown two real examples where I have shown how to create a complete folder structure from a CSV file.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.