Created
September 24, 2022 23:28
-
-
Save josepsmartinez/1f36425c94c87df35f83c230743e770a to your computer and use it in GitHub Desktop.
[Windows] Force removal of directory (Powershell 7.2)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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