Page MenuHomePhorge

Deleting
Updated 215 Days AgoPublic

Windows makes it annoyingly hard to actually wipe files and folders sometimes. It's not even that it's insisting on high permissions, it's that it's finicky and fiddly to apply the right permissions.

The main two commands to use for permissions are takeown to take ownership, and icacls to change the ACL specifics to allow you to actually delete things. I suppose maybe these are useful for other things but when I'm on Windows I'm generally slashing and burning . . .

example recursive deletion
# Assuming we pass the path (relative or absolute) to the folder to be deleted as the only argument to a PowerShell script
$f=$args[0]
# Use takeown to recursively give ownership to a folder and its contents to the Adminstrators group
takeown /A /R /F "$f"
# Then use icacls to recursively (via /T) grant the admin group full permissions
icacls "$f" /grant Administrators:F /T
# If this Remove-Item invocation doesn't work, try appending "\*" to it, sometimes that works (though it doesn't delete the folder itself then) and sometimes it still fails but gives a more accurate error message
Remove-item -Recurse -Force "$f"
Last Author
keithzg
Last Edited
Feb 5 2024, 2:05 AM