A quick powershell function to use Robocopy to delete a full directory.
Deleting folders using Robocopy is a quicker, more robust method of deleting directories. It can also deal with the ‘path too long’ issue encountered by File Explorer’s directory deletion.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function RoboDelete{ | |
param ( | |
$Path | |
) | |
##– generate a randomly named folder –## | |
$randomString = -join ((48..57) + (97..122) | Get-Random -Count 32 | % {[char]$_}) | |
##– make the empty directory –## | |
New-Item -ItemType Directory -Path $randomString -Verbose | |
$emptyDirectory = Get-Item -Path $randomString -Force -Verbose | |
$emptyDirectory.Attributes = 'Hidden' | |
##– robocopy delete the directory –## | |
Robocopy.exe $randomString $Path /MIR /MT:128 | |
##– delete the empty directory –## | |
Remove-Item -Path $randomString -Force -Verbose -Recurse | |
##– delete the now empty directory we were deleting –## | |
Remove-Item -Path $Path -Force -Verbose -Recurse | |
} |
Usage
RoboDelete -Path <path-to-folder>
0 Comments