hifilo
hifilo

Reputation: 43

powershell won't remove multiple directories

I am relatively new to power-shell. I can delete folders individually, but I can't delete multiple folders, nor can I use a loop.

rmdir 'folder 1' -Recurse -Force in the folder works.

rmdir 'folder 1' 'folder 2' -Recurse -Force does not.

these also don't work.

$filenames = Get-Content -Path ".\delete.txt"
foreach ($file in $filenames) {
    Remove-Item -Force -Recurse -Path "./$file"
}
$filenames = Get-Content -Path ".\delete.txt"
foreach ($file in $filenames) {
Remove-Item "./$file" -Force -Recurse -Path
}

What am I missing?

Upvotes: 2

Views: 1526

Answers (1)

Chester Ayala
Chester Ayala

Reputation: 228

Please use comma (,) if you have two or more folders to remove.

Remove-Item -Path "folder 1", "folder 2" -Recurse -Force

The first code block with the foreach loop that you provided is working fine. Just make sure that the files in delete.txt are all in the current directory. Otherwise, just use full paths in delete.txt and replace "./$file" to $file.

Upvotes: 3

Related Questions