Note my script examples are primarily for my use and may not be the best examples of how to do something effectively and efficiently. I don't have native scripting skills so I work from other examples and brute force but they get the job done for me. If you have a suggestion on ways to improve something comments are welcome negative or positive; if negative at least mention what can improve the script.
These examples build new directories or if they exist empty the contents so they are fresh
#Declare some variables (optional just makes reuse easier)
$MyFolderHome=”c:\myfolder”
$MySubFolder1=”c:\myfolder\subfolder1”
$MySubFolder2=”c:\myfolder\subfolder2”
$DirList=”c:\dirlist”
if (! (test-path $MyFolderHome)) If this dir does not exist
{mkdir $MyFolderHome} add the dir
else {remove-item $MyFolderHome\* -recurse -force} if it does exist clean it out
if (!(test-path $MySubFolder1))
{mkdir $MySubFolder1}
else {remove-item $MySubFolder1\* -recurse -force}
if (!(test-path $MySubFolder2))
{mkdir $MySubFolder2}
else {remove-item $MySubFolder2\* -recurse -force}
Now suppose you have a list called dirlist with some sub directories you want to add to the sub folders you just created and the list contains apples, oranges, and pears you could add them to each folder using a small loop.
foreach ($a in cat $DirList)
{
mkdir $MySubFolder1\$a
mkdir $MySubFolder2\$a
}
The end product will look like:
c:\myfolder\subfolder1\apples c:\myfolder\subfolder2\pears etc.
_________________Full Script________________________________
#Declare some variables
$MyFolderHome=”c:\myfolder”
$MySubFolder1=”c:\myfolder\subfolder1”
$MySubFolder2=”c:\myfolder\subfolder2”
$DirList=”c:\dirlist”
#create the directories
if (! (test-path $MyFolderHome))
{mkdir $MyFolderHome}
else {remove-item $MyFolderHome\* -recurse -force}
if (!(test-path $MySubFolder1))
{mkdir $MySubFolder1}
else {remove-item $MySubFolder1\* -recurse -force}
if (!(test-path $MySubFolder2))
{mkdir $MySubFolder2}
else {remove-item $MySubFolder2\* -recurse -force}
#add directories from the list to each sub-directory created above
foreach ($a in cat $DirList)
{
mkdir $MySubFolder1\$a
mkdir $MySubFolder2\$a
}