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.

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
}
view raw RoboDelete.ps1 hosted with ❤ by GitHub

Usage

RoboDelete -Path <path-to-folder>
Categories: Powershell

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.