PowerShell Script For Creating Same Folders In Multiple Directories









up vote
-1
down vote

favorite












Current folder structure:



\ServerSourceA1A1 101
\ServerSourceA1A1 102

\ServerSourceA2A2 101
\ServerSourceA2A2 102

\ServerSourceA3A3 101
\ServerSourceA3A3 102


I need to be able to create 3 folders, A, B, and C, inside each of the second level folders under source (A1 101, A2 101, etc.). FolderDir.txt has A, B, C on their own line. Is there a better way other than the following? There are a LOT more sub directories than listed above:



Foreach($folder in Get-Content "C:tempfolderDir.txt")

New-Item "\ServerSourceA1A1 101$folder" -ItemType directory
New-Item "\ServerSourceA1A1 102$folder" -ItemType directory
New-Item "\ServerSourceA2A2 101$folder" -ItemType directory
New-Item "\ServerSourceA2A2 102$folder" -ItemType directory
New-Item "\ServerSourceA3A3 101$folder" -ItemType directory
New-Item "\ServerSourceA3A3 102$folder" -ItemType directory



Thanks! I'm still pretty new to PowerShell.



This is what I came up with:



Foreach($folder in Get-Content "C:tempfolderDir.txt")

$lesson = Get-ChildItem -Path \ServerSourceA**
New-Item $lesson$folder -ItemType Directory



But I'm getting the error:



New-Item : Could not find a part of the path 'A'.
At line:4 char:1
+ New-Item $lesson$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\ServerSource...A:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : Could not find a part of the path 'B'.
At line:4 char:1
+ New-Item $lesson$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\ServerSource...B:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : Could not find a part of the path 'C'.
At line:4 char:1
+ New-Item $lesson$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\ServerSource...C:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand


However, if I put -WhatIf command on the end (after "directory") it appears to be able to do what I want...










share|improve this question









New contributor




JPS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    "I've yet to find a way to do this" Can you post the code you have so far and point out where it is failing?
    – boxdog
    Nov 9 at 13:36










  • You are a bit unclear if you want to create the subfolders in every 2nd level folder below source or only the ones ending in 101,102. Instead of literally writing everyone single one, use Get-CildItem to iterate the folder structure and a ForEach-Object to issue the New-Item in each one.
    – LotPings
    Nov 9 at 14:39










  • Apologies. Every 2nd level folder under source. The numbers vary. How would I write that, with Get-ChildItem, for it to place those new directories in every second level directory?
    – JPS
    Nov 9 at 14:54















up vote
-1
down vote

favorite












Current folder structure:



\ServerSourceA1A1 101
\ServerSourceA1A1 102

\ServerSourceA2A2 101
\ServerSourceA2A2 102

\ServerSourceA3A3 101
\ServerSourceA3A3 102


I need to be able to create 3 folders, A, B, and C, inside each of the second level folders under source (A1 101, A2 101, etc.). FolderDir.txt has A, B, C on their own line. Is there a better way other than the following? There are a LOT more sub directories than listed above:



Foreach($folder in Get-Content "C:tempfolderDir.txt")

New-Item "\ServerSourceA1A1 101$folder" -ItemType directory
New-Item "\ServerSourceA1A1 102$folder" -ItemType directory
New-Item "\ServerSourceA2A2 101$folder" -ItemType directory
New-Item "\ServerSourceA2A2 102$folder" -ItemType directory
New-Item "\ServerSourceA3A3 101$folder" -ItemType directory
New-Item "\ServerSourceA3A3 102$folder" -ItemType directory



Thanks! I'm still pretty new to PowerShell.



This is what I came up with:



Foreach($folder in Get-Content "C:tempfolderDir.txt")

$lesson = Get-ChildItem -Path \ServerSourceA**
New-Item $lesson$folder -ItemType Directory



But I'm getting the error:



New-Item : Could not find a part of the path 'A'.
At line:4 char:1
+ New-Item $lesson$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\ServerSource...A:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : Could not find a part of the path 'B'.
At line:4 char:1
+ New-Item $lesson$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\ServerSource...B:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : Could not find a part of the path 'C'.
At line:4 char:1
+ New-Item $lesson$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\ServerSource...C:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand


However, if I put -WhatIf command on the end (after "directory") it appears to be able to do what I want...










share|improve this question









New contributor




JPS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    "I've yet to find a way to do this" Can you post the code you have so far and point out where it is failing?
    – boxdog
    Nov 9 at 13:36










  • You are a bit unclear if you want to create the subfolders in every 2nd level folder below source or only the ones ending in 101,102. Instead of literally writing everyone single one, use Get-CildItem to iterate the folder structure and a ForEach-Object to issue the New-Item in each one.
    – LotPings
    Nov 9 at 14:39










  • Apologies. Every 2nd level folder under source. The numbers vary. How would I write that, with Get-ChildItem, for it to place those new directories in every second level directory?
    – JPS
    Nov 9 at 14:54













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











Current folder structure:



\ServerSourceA1A1 101
\ServerSourceA1A1 102

\ServerSourceA2A2 101
\ServerSourceA2A2 102

\ServerSourceA3A3 101
\ServerSourceA3A3 102


I need to be able to create 3 folders, A, B, and C, inside each of the second level folders under source (A1 101, A2 101, etc.). FolderDir.txt has A, B, C on their own line. Is there a better way other than the following? There are a LOT more sub directories than listed above:



Foreach($folder in Get-Content "C:tempfolderDir.txt")

New-Item "\ServerSourceA1A1 101$folder" -ItemType directory
New-Item "\ServerSourceA1A1 102$folder" -ItemType directory
New-Item "\ServerSourceA2A2 101$folder" -ItemType directory
New-Item "\ServerSourceA2A2 102$folder" -ItemType directory
New-Item "\ServerSourceA3A3 101$folder" -ItemType directory
New-Item "\ServerSourceA3A3 102$folder" -ItemType directory



Thanks! I'm still pretty new to PowerShell.



This is what I came up with:



Foreach($folder in Get-Content "C:tempfolderDir.txt")

$lesson = Get-ChildItem -Path \ServerSourceA**
New-Item $lesson$folder -ItemType Directory



But I'm getting the error:



New-Item : Could not find a part of the path 'A'.
At line:4 char:1
+ New-Item $lesson$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\ServerSource...A:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : Could not find a part of the path 'B'.
At line:4 char:1
+ New-Item $lesson$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\ServerSource...B:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : Could not find a part of the path 'C'.
At line:4 char:1
+ New-Item $lesson$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\ServerSource...C:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand


However, if I put -WhatIf command on the end (after "directory") it appears to be able to do what I want...










share|improve this question









New contributor




JPS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











Current folder structure:



\ServerSourceA1A1 101
\ServerSourceA1A1 102

\ServerSourceA2A2 101
\ServerSourceA2A2 102

\ServerSourceA3A3 101
\ServerSourceA3A3 102


I need to be able to create 3 folders, A, B, and C, inside each of the second level folders under source (A1 101, A2 101, etc.). FolderDir.txt has A, B, C on their own line. Is there a better way other than the following? There are a LOT more sub directories than listed above:



Foreach($folder in Get-Content "C:tempfolderDir.txt")

New-Item "\ServerSourceA1A1 101$folder" -ItemType directory
New-Item "\ServerSourceA1A1 102$folder" -ItemType directory
New-Item "\ServerSourceA2A2 101$folder" -ItemType directory
New-Item "\ServerSourceA2A2 102$folder" -ItemType directory
New-Item "\ServerSourceA3A3 101$folder" -ItemType directory
New-Item "\ServerSourceA3A3 102$folder" -ItemType directory



Thanks! I'm still pretty new to PowerShell.



This is what I came up with:



Foreach($folder in Get-Content "C:tempfolderDir.txt")

$lesson = Get-ChildItem -Path \ServerSourceA**
New-Item $lesson$folder -ItemType Directory



But I'm getting the error:



New-Item : Could not find a part of the path 'A'.
At line:4 char:1
+ New-Item $lesson$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\ServerSource...A:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : Could not find a part of the path 'B'.
At line:4 char:1
+ New-Item $lesson$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\ServerSource...B:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand

New-Item : Could not find a part of the path 'C'.
At line:4 char:1
+ New-Item $lesson$folder -ItemType Directory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (\ServerSource...C:String) [New-Item], DirectoryNotFoundException
+ FullyQualifiedErrorId : CreateDirectoryIOError,Microsoft.PowerShell.Commands.NewItemCommand


However, if I put -WhatIf command on the end (after "directory") it appears to be able to do what I want...







windows powershell windows-10






share|improve this question









New contributor




JPS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




JPS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Nov 9 at 16:38





















New contributor




JPS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Nov 9 at 13:18









JPS

82




82




New contributor




JPS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





JPS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






JPS is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 2




    "I've yet to find a way to do this" Can you post the code you have so far and point out where it is failing?
    – boxdog
    Nov 9 at 13:36










  • You are a bit unclear if you want to create the subfolders in every 2nd level folder below source or only the ones ending in 101,102. Instead of literally writing everyone single one, use Get-CildItem to iterate the folder structure and a ForEach-Object to issue the New-Item in each one.
    – LotPings
    Nov 9 at 14:39










  • Apologies. Every 2nd level folder under source. The numbers vary. How would I write that, with Get-ChildItem, for it to place those new directories in every second level directory?
    – JPS
    Nov 9 at 14:54













  • 2




    "I've yet to find a way to do this" Can you post the code you have so far and point out where it is failing?
    – boxdog
    Nov 9 at 13:36










  • You are a bit unclear if you want to create the subfolders in every 2nd level folder below source or only the ones ending in 101,102. Instead of literally writing everyone single one, use Get-CildItem to iterate the folder structure and a ForEach-Object to issue the New-Item in each one.
    – LotPings
    Nov 9 at 14:39










  • Apologies. Every 2nd level folder under source. The numbers vary. How would I write that, with Get-ChildItem, for it to place those new directories in every second level directory?
    – JPS
    Nov 9 at 14:54








2




2




"I've yet to find a way to do this" Can you post the code you have so far and point out where it is failing?
– boxdog
Nov 9 at 13:36




"I've yet to find a way to do this" Can you post the code you have so far and point out where it is failing?
– boxdog
Nov 9 at 13:36












You are a bit unclear if you want to create the subfolders in every 2nd level folder below source or only the ones ending in 101,102. Instead of literally writing everyone single one, use Get-CildItem to iterate the folder structure and a ForEach-Object to issue the New-Item in each one.
– LotPings
Nov 9 at 14:39




You are a bit unclear if you want to create the subfolders in every 2nd level folder below source or only the ones ending in 101,102. Instead of literally writing everyone single one, use Get-CildItem to iterate the folder structure and a ForEach-Object to issue the New-Item in each one.
– LotPings
Nov 9 at 14:39












Apologies. Every 2nd level folder under source. The numbers vary. How would I write that, with Get-ChildItem, for it to place those new directories in every second level directory?
– JPS
Nov 9 at 14:54





Apologies. Every 2nd level folder under source. The numbers vary. How would I write that, with Get-ChildItem, for it to place those new directories in every second level directory?
– JPS
Nov 9 at 14:54













1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Because the folderlist is iterated over several times it should be read in first



untested:



$folderlist = Get-Content "C:tempfolderDir.txt"
Get-ChildItem -Path '\ServerSource**' -Directory | ForEach-Object
ForEach ($folder in $folderlist)
New-Item (Join-Path $_.FullName $folder) -ItemType directory -Force -WhatIf




If the output look OK remove the -WhatIf






share|improve this answer




















  • That works great! Thanks!!
    – JPS
    Nov 9 at 17:29










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






JPS is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226462%2fpowershell-script-for-creating-same-folders-in-multiple-directories%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote



accepted










Because the folderlist is iterated over several times it should be read in first



untested:



$folderlist = Get-Content "C:tempfolderDir.txt"
Get-ChildItem -Path '\ServerSource**' -Directory | ForEach-Object
ForEach ($folder in $folderlist)
New-Item (Join-Path $_.FullName $folder) -ItemType directory -Force -WhatIf




If the output look OK remove the -WhatIf






share|improve this answer




















  • That works great! Thanks!!
    – JPS
    Nov 9 at 17:29














up vote
0
down vote



accepted










Because the folderlist is iterated over several times it should be read in first



untested:



$folderlist = Get-Content "C:tempfolderDir.txt"
Get-ChildItem -Path '\ServerSource**' -Directory | ForEach-Object
ForEach ($folder in $folderlist)
New-Item (Join-Path $_.FullName $folder) -ItemType directory -Force -WhatIf




If the output look OK remove the -WhatIf






share|improve this answer




















  • That works great! Thanks!!
    – JPS
    Nov 9 at 17:29












up vote
0
down vote



accepted







up vote
0
down vote



accepted






Because the folderlist is iterated over several times it should be read in first



untested:



$folderlist = Get-Content "C:tempfolderDir.txt"
Get-ChildItem -Path '\ServerSource**' -Directory | ForEach-Object
ForEach ($folder in $folderlist)
New-Item (Join-Path $_.FullName $folder) -ItemType directory -Force -WhatIf




If the output look OK remove the -WhatIf






share|improve this answer












Because the folderlist is iterated over several times it should be read in first



untested:



$folderlist = Get-Content "C:tempfolderDir.txt"
Get-ChildItem -Path '\ServerSource**' -Directory | ForEach-Object
ForEach ($folder in $folderlist)
New-Item (Join-Path $_.FullName $folder) -ItemType directory -Force -WhatIf




If the output look OK remove the -WhatIf







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 9 at 17:08









LotPings

15.4k61531




15.4k61531











  • That works great! Thanks!!
    – JPS
    Nov 9 at 17:29
















  • That works great! Thanks!!
    – JPS
    Nov 9 at 17:29















That works great! Thanks!!
– JPS
Nov 9 at 17:29




That works great! Thanks!!
– JPS
Nov 9 at 17:29










JPS is a new contributor. Be nice, and check out our Code of Conduct.









 

draft saved


draft discarded


















JPS is a new contributor. Be nice, and check out our Code of Conduct.












JPS is a new contributor. Be nice, and check out our Code of Conduct.











JPS is a new contributor. Be nice, and check out our Code of Conduct.













 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226462%2fpowershell-script-for-creating-same-folders-in-multiple-directories%23new-answer', 'question_page');

);

Post as a guest














































































Popular posts from this blog

Darth Vader #20

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Ondo