# WARNING: THIS IS A VERY DESTRUCTIVE FUNCTION. IF EXECUTED AT THE WRONG DIRECTORY WHERE IMPORTANT FILES RESIDE, THE SYSTEM WILL BE DESTROYED!!! CAVEAT EMPTOR! BE CAREFUL! THINK BEFORE ACTING!
function purgeDirectory($path='c:\temp'){
function confirmation($content,$testValue="I confirm",$maxAttempts=3){
$confirmed=$false;
$attempts=0;
$content|write-host
write-host "Please review this content for accuracy.`r`n"
while ($attempts -le $maxAttempts){
if($attempts++ -ge $maxAttempts){
write-host "A maximum number of attempts have reached. No confirmations received!`r`n"
break;
}
$userInput = Read-Host -Prompt "Please type in this value => $testValue <= to confirm. Input CANCEL to skip this item.";
if ($userInput.ToLower() -eq $testValue.ToLower()){
$confirmed=$true;
write-host "Confirmed!`r`n";
break;
}elseif($userInput -like 'cancel'){
write-host 'Cancel command received.'
$confirmed=$false
break
}else{
cls;
$content|write-host
write-host "Attempt number $attempts of $maxAttempts`: $userInput does not match $testValue. Try again or Input CANCEL to skip this item`r`n"
}
}
return $confirmed;
}
write-host "Taking ownership and granting Administrators full access to $path..."
$null=takeown /F $path /A /R /D Y
$null=icacls $path /Grant Administrators:F /inheritance:e /T
$emptyDirectory="C:\emptyDirectory"
md -Force $emptyDirectory
Remove-Item "$emptyDirectory`\*" -force -recurse -ErrorAction Continue
$confirmed=confirmation "This will delete all files and folders from $path"
if ($confirmed){
write-host "Now purging ALL files and folders inside $path..." -foregroundcolor yellow
$null=robocopy $emptyDirectory $path /mir /R:0 /W:0 /NP
# rmdir -Force $path
}else{
write-host "Purging has been cancelled for $path"
}
}
purgeDirectory 'c:\temp'
Categories: