Remove-Item $folder -recurse -force
sometimes does not remove all the files. In some cases it yields the message, that a folder can't be deleted, because it is not empty, but when checking the folder is empty. In other not all files are removed.
To reach a clean state, I wrote the following script:
function Remove-ItemRecursiveBruteForce { param( [Parameter(Position = 0, Mandatory=$true, ValueFromPipeline=$false)] [string]$folder, [Parameter(Position = 1, Mandatory=$False, ValueFromPipeline=$false)] $maxiterations = 20 ) $iteration = 0 while ( $iteration++ -lt $maxiterations) { if (Test-Path $folder) { Remove-Item $folder -recurse -force -EA SilentlyContinue } else { "$folder deleted in $iteration iterations" break } } if (Test-Path $folder) { "$folder not empty after $iteration iterations" } }
I observe that it needs randomly between 2 and 11 iterations to remove my folder.
I'm using W7-32 bit and use PowerShell-ISE to run the script.
Is this problem just me, or is it OS specific. Any explanations?
Hey,
AntwortenLöschenI'm having the same problem... Also running Windows7 32bit...
Did you find a different way to recursively delete a directory?
Thanks,
Tiberiu
Btw, here's how I'm doing it:
AntwortenLöschencmd /c "rd /s /q $dir_name"
After working with Bash, TCL, Ruby and Korn I'm realizing that Powershell is a piece of crap.
I've noticed that it won't work using the ISE but it will work if you type it in on the PS Command Line - weird!
AntwortenLöschenI noticed that problem when I delete a file. I fixed it as you can see in the code below:
AntwortenLöschen$myitem = $file.FullName
Remove-Item "$myitem"