Skip to content

Instantly share code, notes, and snippets.

@milnak
Created April 19, 2024 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milnak/e986c954070074a85e796b4a4d7c2add to your computer and use it in GitHub Desktop.
Save milnak/e986c954070074a85e796b4a4d7c2add to your computer and use it in GitHub Desktop.
Take ownership of a Windows file or folder
@ECHO OFF
FOR /f "usebackq" %%f IN (`whoami.exe /priv`) DO IF %%f.==SeTakeOwnershipPrivilege. GOTO :CHKARGS
ECHO ERROR: This script must be run as an administrator!
GOTO :EOF
:CHKARGS
IF "%~1"=="" GOTO :EOF
ECHO About to take ownership of "%~1" . . .
PAUSE
takeown.exe /f "%~1" /r /d y
icacls.exe "%~1" /grant administrators:F /t
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment