How to make this more simplify?

#Full path of the folder
$folder1 = ‘c:\users\backupnt04\downloads’

If the folder exists, show message it exist.

if (Test-Path -Path $folder1 -PathType Container) {
try {Write-Host “Checked: The folder [$folder1] exist.”
} catch {throw $_.Exception.Message}
}

If the file does not exist, show a message it doesn’t exist.

else {Write-Host “Checked: The folder [$folder1] does not exist.”
}

$folder2 = ‘c:\users\backupnt04\desktop’

if (Test-Path -Path $folder2 -PathType Container) {
try {Write-Host “Checked: The folder [$folder2] exist.”
} catch {throw $_.Exception.Message}
}

else {Write-Host “Checked: The folder [$folder2] does not exist.”
}

$folder3 = ‘c:\users\shicorp\downloads’

if (Test-Path -Path $folder3 -PathType Container) {
try {Write-Host “Checked: The folder [$folder3] exist.”
} catch {throw $_.Exception.Message}
}

else {Write-Host “Checked: The folder [$folder3] does not exist.”
}

$folder4 = ‘c:\users\shicorp\desktop’

if (Test-Path -Path $folder4 -PathType Container) {
try {Write-Host “Checked: The folder [$folder3] exist.”
} catch {throw $_.Exception.Message}
}

else {Write-Host “Checked: The folder [$folder4] does not exist.”
}

$folder5 = ‘c:\users\cfahey\downloads’

if (Test-Path -Path $folder5 -PathType Container) {
try {Write-Host “Checked: The folder [$folder3] exist.”
} catch {throw $_.Exception.Message}
}

else {Write-Host “Checked: The folder [$folder5] does not exist.”
}

$folder6 = ‘c:\users\cfahey\desktop’

if (Test-Path -Path $folder6 -PathType Container) {
try {Write-Host “Checked: The folder [$folder3] exist.”
} catch {throw $_.Exception.Message}
}

else {Write-Host “Checked: The folder [$folder6] does not exist.”
}

$folder7 = ‘c:\users\jburke\downloads’

if (Test-Path -Path $folder7 -PathType Container) {
try {Write-Host “Checked: The folder [$folder3] exist.”
} catch {throw $_.Exception.Message}
}

else {Write-Host “Checked: The folder [$folder7] does not exist.”
}

# List users then test path
$user = @('backupnt04','shicorp','cfahey','jburke')
switch ($user)
{
    {Test-Path "C:\Users\$_\Desktop"} {"Checked: C:\Users\$_\Desktop exists"}
    {Test-Path "C:\Users\$_\Downloads"} {"Checked: C:\Users\$_\Downloads exists"}
    {(!(Test-Path "C:\Users\$_\Desktop"))} {"Checked: C:\Users\$_\Desktop does not exists"}
    {(!(Test-Path "C:\Users\$_\Downloads"))} {"Checked: C:\Users\$_\Downloads does not exists"}
}
2 Likes

Awesome. Thank you very much :heart_eyes: