I have a drive that contains a list of clients, each client contains a folder that is used to store specific tasks for each month within another folder for each month.
The directory looks something like this:
I:\Clients\Client Name\PM Tasks\Month
I am attempting to write a script that will generate each month 01-12 within the bottom folder for each client so that I don't have to each client, which is about 200, and create 12 new folders in each folder.
I have seen this code
$subfolders = "Subfolder1", "Subfolder2", "Subfolder3" # Add or modify subfolders as needed
foreach ($subfolder in $subfolders) {
$fullPath = Join-Path $networkPath $subfolder
if (!(Test-Path -Path $fullPath)) {
New-Item -ItemType Directory -Path $fullPath
Write-Host "Created directory: $fullPath"
} else {
Write-Host "Directory already exists: $fullPath"
}
}
But I am not sure how I would get each client name and use it as the network path. Would I use Get-ChildItem store it as a variable and then just add it to the network path? The Subfolder will always be the same Client\PM Tasks
Thanks in advance
Update:
I have been able to create a new test folder within the network drive using the following code:
$networkPath = "I:\Clients\"
$folders = Get-ChildItem -Path $networkPath | Select Name
foreach ($folder in $folders){
Write-Output "Folder Name: " $folder
$newPath = $networkPath + $folder.Name + "\PM Tasks\Test"
if(!(Test-Path -Path $newPath)){
New-Item -ItemType Directory -Path $newPath
Write-Host "Created Directory: $newPath"
} else {
Write-Host "Directory already exists: $newPath"\
}
}
Now I just need to figure out how to create the 12 months in each directory
I have a drive that contains a list of clients, each client contains a folder that is used to store specific tasks for each month within another folder for each month.
The directory looks something like this:
I:\Clients\Client Name\PM Tasks\Month
I am attempting to write a script that will generate each month 01-12 within the bottom folder for each client so that I don't have to each client, which is about 200, and create 12 new folders in each folder.
I have seen this code
$subfolders = "Subfolder1", "Subfolder2", "Subfolder3" # Add or modify subfolders as needed
foreach ($subfolder in $subfolders) {
$fullPath = Join-Path $networkPath $subfolder
if (!(Test-Path -Path $fullPath)) {
New-Item -ItemType Directory -Path $fullPath
Write-Host "Created directory: $fullPath"
} else {
Write-Host "Directory already exists: $fullPath"
}
}
But I am not sure how I would get each client name and use it as the network path. Would I use Get-ChildItem store it as a variable and then just add it to the network path? The Subfolder will always be the same Client\PM Tasks
Thanks in advance
Update:
I have been able to create a new test folder within the network drive using the following code:
$networkPath = "I:\Clients\"
$folders = Get-ChildItem -Path $networkPath | Select Name
foreach ($folder in $folders){
Write-Output "Folder Name: " $folder
$newPath = $networkPath + $folder.Name + "\PM Tasks\Test"
if(!(Test-Path -Path $newPath)){
New-Item -ItemType Directory -Path $newPath
Write-Host "Created Directory: $newPath"
} else {
Write-Host "Directory already exists: $newPath"\
}
}
Now I just need to figure out how to create the 12 months in each directory
Share edited Mar 4 at 5:40 kevorski asked Mar 4 at 5:11 kevorskikevorski 8481 gold badge13 silver badges31 bronze badges2 Answers
Reset to default 2Loop over all client directories, assumed to be subdirectories of
$networkPath
In each client directory, create a
PM Tasks
subfolder.In each such subfolder, use
..
, the range operator, to enumerate the numbers1
through12
, and create a subfolder for each 2-digit zero-padded number (the padding is created via-f
, the format operator).
$networkPath = 'I:\Clients\'
Get-ChildItem -LiteralPath $networkPath -Directory |
ForEach-Object {
$tasksDir = New-Item -Type Directory -Force -Path $_.FullName -Name 'PM Tasks'
1..12 |
ForEach-Object {
$null = New-Item -Type Directory -Force -Path $tasksDir.FullName -Name ('{0:D2}' -f $_)
}
}
Note that the use of -Force
with New-Item -Type Directory
makes the operation idempotent; that is, if the target directory already exists, no action is taken, and a [System.IO.DirectoryInfo]
instance describing the preexisting directory is returned.
If you want to create the folders on various networkdrives you can store the routes on a txt and loop through them. I dont know if you want the months by names or numbers so you can change it in the array.
$routnetPath = Get-Content "C:\Scripts\NETWPATH.txt"
foreach ($networkPath in $routnetPath){
$months = @("January","February","March",...)
foreach ($month in $months) {
$newfolderroute = Join-Path -Path $networkPath -ChildPath $month
if (-not (Test-Path -Path $newfolderroute)) {
New-Item -Path $newfolderroute -ItemType Directory
Write-Host "Folder $month was created in $networkPath"
} else {
Write-Host "The folder $month already exists"
}
}
}