Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to quietly remove a directory with content in PowerShell

People also ask

How do I delete a folder and its contents in PowerShell?

Use the Delete() Method Every object in PowerShell has a Delete() method and you can use it to remove that object. To delete files and folders, use the Get-ChildItem command and use the Delete() method on the output.

How do you remove a directory and all of its contents?

To remove a directory and all its contents, including any subdirectories and files, use the rm command with the recursive option, -r . Directories that are removed with the rmdir command cannot be recovered, nor can directories and their contents removed with the rm -r command.

How do I remove non empty directory in PowerShell?

Open PowerShell by pressing the Start button and typing PowerShell. Press Enter. Type Remove-Item –path c:\testfolder –recurse and hit Enter.

How do I force delete a folder in PowerShell?

In the PowerShell console, type Remove-Item –path c:\testfolder –recurse and press Enter, replacing c:\testfolder with the full path to the folder you want to delete.


Remove-Item -LiteralPath "foldertodelete" -Force -Recurse

From PowerShell remove force answer: help Remove-Item says:

The Recurse parameter in this cmdlet does not work properly

The command to workaround is

Get-ChildItem -Path $Destination -Recurse | Remove-Item -force -recurse

And then delete the folder itself

Remove-Item $Destination -Force 

This worked for me:

Remove-Item $folderPath -Force  -Recurse -ErrorAction SilentlyContinue

Thus the folder is removed with all files in there and it is not producing error if folder path doesn't exists.


2018 Update

In the current version of PowerShell (tested with v5.1 on Windows 10 1809) one can use the simpler Unix syntax rm -R .\DirName to silently delete the directory .\DirName with all subdirectories and files it may contain. In fact many common Unix commands work in the same way in PowerShell as in a Linux command line.


To delete content without a folder you can use the following:

Remove-Item "foldertodelete\*" -Force -Recurse

in short, We can use rm -r -fo {folderName} to remove the folder recursively (remove all the files and folders inside) and force