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...
windows powershell windows-10
New contributor
add a comment |
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...
windows powershell windows-10
New contributor
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, useGet-CildItem
to iterate the folder structure and aForEach-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
add a comment |
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...
windows powershell windows-10
New contributor
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
windows powershell windows-10
New contributor
New contributor
edited Nov 9 at 16:38
New contributor
asked Nov 9 at 13:18
JPS
82
82
New contributor
New contributor
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, useGet-CildItem
to iterate the folder structure and aForEach-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
add a comment |
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, useGet-CildItem
to iterate the folder structure and aForEach-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
add a comment |
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
That works great! Thanks!!
– JPS
Nov 9 at 17:29
add a comment |
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
That works great! Thanks!!
– JPS
Nov 9 at 17:29
add a comment |
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
That works great! Thanks!!
– JPS
Nov 9 at 17:29
add a comment |
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
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
answered Nov 9 at 17:08
LotPings
15.4k61531
15.4k61531
That works great! Thanks!!
– JPS
Nov 9 at 17:29
add a comment |
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
add a comment |
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.
JPS is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 aForEach-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