functions/Clear-Folder.ps1
function Clear-Folder { <# .SYNOPSIS This function uses Robocopy to mirror an empty folder over the top of the target directory, it is a VERY quick way of deleting a folder with a lot of subfolders and files. It also bypasses a lot of permission issues. .NOTES Name: Clear-Folder Author: Elliott Marter .EXAMPLE Clear-Folder -TargetDirectory C:\path\to\folder .LINK https://www.powershellgallery.com/profiles/elliottmarter #> [cmdletbinding(SupportsShouldProcess=$True)] param ( [Parameter(Position=0,mandatory=$true)] [string] $TargetDirectory ) # Create empty temp folder $EmptyDir = "$env:TEMP\empty_temp" New-Item -Path $EmptyDir -ItemType Directory -Force Write-Warning "You are about to erase everything in $TargetDirectory are you sure you want to continue" -WarningAction Inquire Write-Warning "Are you ABSOLUTELY certain?" -WarningAction Inquire takeown /a /r /d Y /f $TargetDirectory | Out-Null # use robocopy to mirror the empty directory over the top of the target directory robocopy $EmptyDir $TargetDirectory /MIR | Out-Null # remove empty temp folder and target folder Remove-Item $EmptyDir,$TargetDirectory -Force -Recurse | Out-Null Write-Output "$TargetDirectory has been removed" } |