functions/Clear-Folder.ps1

function Clear-Folder2 {

    [cmdletbinding(SupportsShouldProcess=$True)]
    
    param (
        [Parameter(Position=0,mandatory=$true)]
        [string] $TargetDirectory,
        [switch] $NoWarning
        )

    $EmptyDir = "$env:TEMP\empty_temp"

    # create a empty temp folder for robocopy later
    if (!(Test-Path $EmptyDir)) {
    New-Item -Path $EmptyDir -ItemType Directory
    }

    # warn
    if (!($NoWarning)) {
    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

}