For some reason, the if / else clause does not work. It creates directories, but does not execute the if part, saying "The folder already exists." It must be something super obvious, I donβt see it as a noob. Thank you for your help.
Set-Location "\\domain.net\Target"
$Folders = Import-Csv "C:\Temp\Test.csv"
ForEach ($Folder in $Folders) {
if (Test-Path -LiteralPath $Folder -PathType Container)
{
Write-Host "Folder exists already"
}
else
{
New-Item $Folder.Name -Type Directory
}
}
Example contents of a CSV file:
Name
Folder1
Folder2
Folder3
Folder3\Folder1
The solution thanks to the help of the guys below:
$Target = "\\Temp\Target"
$InputF = "\\Temp\Downloads\Test.csv"
CLS
Write-Host "Folder creation`n"
$Folders = Import-Csv $InputF
Set-Location $Target
Write-Host "`nStarting folder creation:`n"
ForEach ($Folder in $Folders) {
if (Test-Path -LiteralPath $Folder.Name -PathType Container)
{
Write-Host "-Exists: $Target\$($Folder.Name)" -ForegroundColor DarkGray
}
else
{
New-Item $Folder.Name -Type Directory > $null
Write-Host "-Created: $Target\$($Folder.Name)"
}
}
source
share