Create folders from a csv file

Hi everyone: :)
We need to create folders from an csv and want to add subfolders to them. The powerShell script works to create the folder but not to create the subfolders.

content of the csv:
Just to create the main folder (this works fine)
Set-Location "D:\customers"  
$Folders = Import-Csv C:\Temp\foldernames.csv
ForEach ($Folder in $Folders) {
New-Item $Folder.Name -type directory

to create the main folder and the subfolders (this doesn't works)
Set-Location "D:\customers"  
$Folders = Import-Csv C:\Temp\foldernames.csv
ForEach ($Folder in $Folders) {
New-Item $Folder.Name\Subfolder1 -type directory
New-Item $Folder.Name\Subfolder2 -type directory

How should the script look like that it works? (sorry I'm a PowerShell beginner)
Thanks for any help!
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Do you want to create the same subfolders in each "parent"?
Are the parent folders created when you run the second script?
"Subfolder1" and "Subfolder2" are strings, so you need to pass them as such,
And since the folder name is a property, you need to use a subexpression.
And even though the "$var = import-csv; ForEach ..." certainly works, it's better (especially in terms of memory consumption when it comes to larger files) to just use the pipeline.
In the following script, the subexpression around the $Basefolder is technically not required, but for consistency's sake, I prefer to address variables in strings with a subexpression whether required or not.
$BaseFolder = "D:\customers"
Import-Csv -Path "C:\Temp\foldernames.csv" | ForEach-Object {
	New-Item "$($BaseFolder)\$($_.Name)\Subfolder1" -ItemType Directory
	New-Item "$($BaseFolder)\$($_.Name)\Subfolder2" -ItemType Directory

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
oBdA, it works with vars the way as shown by the asker, but your suggestions is "more complete". I would just make sure the parent folder is created. And do the Set-Location, as that allows cleaner code:
Set-Location D:\Customers
Import-Csv -Path "C:\Temp\foldernames.csv" | Select -Expand Name |
   % {
        "$_", "$_\Subfolder1", "$_\Subfolder2") | New-Item -Type Directory -ea SilentlyContinue

Open in new window

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

$Folders = import-csv C:\Temp\foldernames.csv
$Folders | %{
If (!(test-path D:\Project\$($_.Process)))
new-item -type directory -path "C:\Project\$($_.Process)"}
new-item -type directory -path "C:\Project\$($_.Process)\$($_.type)"
It doesn't work as shown by the asker, not with an object property in combination with a string (as in "New-Item $Folder.Name\Subfolder1").
And just as "md", Powershell's New-Item (at least for the filesystem provider) doesn't care whether the parent folder exists or not; it will create the whole tree if required.
Only in good old real DOS (, not the NT shell cmd.exe) was it required to create one folder after the other.
And me, I hate, hate, hate file and folder operations based on the current folder.
I don't think it's cleaner code than using full paths, in contrary - if you don't check whether the Set-Location was successful, and for example the drive doesn't exist, the script will continue and just create the folders wherever you were before.
Personally, I'd do it like this, but I didn't want to deviate too much from the original script:
$BaseFolder = "C:\customers"
$Subfolders = "Subfolder1", "Subfolder2"
Import-Csv -Path "C:\Temp\foldernames.csv" | ForEach-Object {
	ForEach ($Subfolder In $Subfolders) {
		$NewFolder = "$($BaseFolder)\$($_.Name)\$($Subfolder)"
		If (-Not (Test-Path -Path $NewFolder)) {
			New-Item -Path $NewFolder -ItemType Directory

Open in new window

The outer loop with the unknown quantity of file names as pipeline, the inner loop as a PS ForEach to avoid having the confusion with two separate $_ variables in the loops.
windows-itAuthor Commented:
oBdA's solutions works perfect for me. Thanks a lot!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.