3

I am looking to create sub-folders within several different parent folders on a Network share. I have about 900 or so customer folders that I want to add a sub-folder to. I have the clients listed in an excel spreadsheet, which is not a CSV, is there an easy way to create these sub-folders?

New-Item -ItemType directory -Path "P:\Customer Folders\Customers A thru D\

I am using the above as directory creation but I don't want to have to use this command 900+ times. -recurse doesn't seem to do it as it gives me an error.

Any help would be appreciated.

4
  • Have you considered ForEach($Folder in (Get-ChildItem 'P:\Customer Folders' -Directory)){New-Item -ItemType Directory -Path $Folder.FullName+"\Folder to create"}? Or maybe saving your excel sheet as a CSV and going from there? Commented Sep 22, 2014 at 18:50
  • The excel sheet isn't formatted properly for that, it won't import the data properly. With the .FullName+ would that allow me to just run it and it would create the folder in all subfolders? How would I input the client names? I am still a little green with PowerShell. Could I create it in one spot then use a -recurse to copy the empty folder to each one after that? Commented Sep 22, 2014 at 18:53
  • Without having a better idea of your directory structure it is hard to answer those questions. My suggested code would created a folder named 'Folder to create' inside every subfolder of 'P:\Customer Folders'. So for example: 'P:\Customer Folders\Microsoft\Folder to create', 'P:\Customer Folders\Starbucks\Folder to create', 'P:\Customer Folders\Boeing\Folder to create' Commented Sep 22, 2014 at 19:03
  • To give you a better idea, the structure is P:\Customer Folders\Customers A thru D\"Customer Name"\HelpDesk. I am trying to create a HelpDesk sub-folder for each customer folder. I tried to use the one you supplied above on a small test structure and I am getting an error - + CategoryInfo : InvalidArgument: (:) [New-Item], ParameterBindingException + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.NewItemCommand Commented Sep 22, 2014 at 19:10

2 Answers 2

4

Ok, I forgot parenthesis when I gave the code in my comment. Here's what should work for you:

foreach($folder in (gci 'P:\customers a thru d' -directory)){
    new-item -ItemType directory -Path ($folder.fullname+"\Helpdesk")
}

I created the following to test with:

C:\Temp\customers a thru d\Microsoft
C:\Temp\customers a thru d\Starbucks
C:\Temp\customers a thru d\boeing

I ran the code above (with path modified as appropriate), and it created the folders as expected. If you want to suppress the output you can pipe that to |Out-Null, but that's up to you.

Sign up to request clarification or add additional context in comments.

1 Comment

Dude, you just made my day. Thank you so much, it works like a charm. I marked it as an answer sadly I don't have the reputation to upvote other wise I would.
0

The directory parameter for the Get-ChildItem needs Powershell version 3.0. I got the following to work with v1.

Get-ChildItem -path C:\Test | #replace C:\Test with your structure
Where-Object { $_.PSIsContainer } |
ForEach-Object {
    #Write-host $_.FullName
    new-item ($_.FullName+"\Folder Name Here") -type directory  
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.