Skip to content

Instantly share code, notes, and snippets.

@josepsmartinez
Created September 24, 2022 23:28
Show Gist options
  • Save josepsmartinez/1f36425c94c87df35f83c230743e770a to your computer and use it in GitHub Desktop.
Save josepsmartinez/1f36425c94c87df35f83c230743e770a to your computer and use it in GitHub Desktop.
[Windows] Force removal of directory (Powershell 7.2)
# Run code below in a shell started by the following command
# to guarantee Administrator privileges
# Start-Process powershell -Verb runAs
function Control-Item-Cmd {
param($InputObject)
$InputObjectPath = $InputObject.fullname
$GrantArg = "{0}:(F)" -f $env:USERNAME
takeown /F $InputObjectPath /R
icacls $InputObjectPath /T /grant $GrantArg
}
function Remove-Item-Forcefully {
$args | ForEach-Object {
$InputObject = Get-item $_
Control-Item-Cmd $InputObject
Remove-Item $InputObject -Recurse -Force
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment