2

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

It does not work if the folder paths are stored in variables.
Store your folder paths in an array variable like this $filenames = ("C:\folder1", "C:\folder2"). Then you can use the for each loop block or this one-line answer. Remove-Item -Path $filenames -Recurse -Force

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.