Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 526 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to recursively delete an entire directory with PowerShell 2.0?

#1
What is the simplest way to forcefully delete a directory and all its subdirectories in PowerShell? I am using PowerShell V2 in Windows 7.

I have learned from several sources that the most obvious command, `Remove-Item $targetDir -Recurse -Force`, does not work correctly. This includes a statement in the PowerShell V2 online help (found using `Get-Help Remove-Item -Examples`) that states:

> ...Because the Recurse parameter in this cmdlet is faulty, the command uses the Get-Childitem cmdlet to get the desired files, and it uses the pipeline operator to pass them to the Remove-Item cmdlet...

I have seen various examples that use **Get-ChildItem** and pipe it to **Remove-Item**, but the examples usually remove some set of files based on a filter, not the entire directory.

I am looking for the cleanest way to blow out an entire directory, files and child directories, without generating any user warning messages using the least amount of code. A one-liner would be nice if it is easy to understand.


Reply

#2
I used:

rm -r folderToDelete

This works for me like a charm (I stole it from Ubuntu).
Reply

#3
Really simple:

remove-item -path <type in file or directory name>, press Enter
Reply

#4
rm -r ./folder -Force

...worked for me
Reply

#5
For some reason John Rees' answer sometimes did not work in my case. But it led me in the following direction.
First I try to delete the directory recursively with the buggy -recurse option. Afterwards I descend into every subdir that's left and delete all files.

function Remove-Tree($Path)
{
Remove-Item $Path -force -Recurse -ErrorAction silentlycontinue

if (Test-Path "$Path\" -ErrorAction silentlycontinue)
{
$folders = Get-ChildItem -Path $Path –Directory -Force
ForEach ($folder in $folders)
{
Remove-Tree $folder.FullName
}

$files = Get-ChildItem -Path $Path -File -Force

ForEach ($file in $files)
{
Remove-Item $file.FullName -force
}

if (Test-Path "$Path\" -ErrorAction silentlycontinue)
{
Remove-Item $Path -force
}
}
}
Reply

#6
Use the old-school DOS command:

rd /s <dir>
Reply

#7
To avoid the "The directory is not empty" errors of the accepted answer, simply use the good old DOS command as suggested before. The full PS syntax ready for copy-pasting is:

& cmd.exe /c rd /S /Q $folderToDelete
Reply

#8
**Another useful trick:**

If you find a lot of files with same or similar name convention (like mac file with dot prefix name... that famous file pulltion), you can easily remove them with one single line from the powershell like this:

ls -r .* | rm

This line is going to remove all files with a dot in the begining of the name inside the current directory, and all files with same circumstances inside other folders inside this directory too. Be aware about it when using it. :D

Reply

#9
I took another approach inspired by @john-rees above - especially when his approach started to fail for me at some point. Basically recurse the subtree and sort files by their path-length - delete from longest to the shortest

Get-ChildItem $tfsLocalPath -Recurse | #Find all children
Select-Object FullName,@{Name='PathLength';Expression={($_.FullName.Length)}} | #Calculate the length of their path
Sort-Object PathLength -Descending | #sort by path length descending
%{ Get-Item -LiteralPath $_.FullName } |
Remove-Item -Force


Regarding the -LiteralPath magic, here's another gotchya that may be hitting you:

[To see links please register here]

Reply

#10
Try this example. If the directory does not exist, no error is raised. You may need PowerShell v3.0.

remove-item -path "c:\Test Temp\Test Folder" -Force -Recurse -ErrorAction SilentlyContinue
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through