<# .SYNOPSIS Advanced Take Ownership Context Menu Installer #> iex (irm psui.pages.dev) $ErrorActionPreference = 'Stop' Write-Host "Preparing Advanced Take Ownership Context Menu..." -ForegroundColor Cyan # 1. Проверка прав администратора и автоматическая элевация $isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) if (-not $isAdmin) { Write-Host "Administrator privileges required. Requesting elevation..." -ForegroundColor Yellow Start-Process powershell -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `"iex (irm takeown.pages.dev)`"" -Verb RunAs pause exit } # 2. Создание временного reg-файла с полным содержимым $regPath = Join-Path $env:TEMP "Takeown_Advanced.reg" $regContent = @' Windows Registry Editor Version 5.00 ; Take_Ownership_Advanced_Context_Menu.reg modified ; ================================================================================================================================= ; 1. Take Ownership: Inherit from parent: YES, Owner = current user, Granted Rights = FullControl (Non-recursive) ; 2. Take Ownership (Recursive): Inherit from parent: YES, Owner = current user, Granted Rights = FullControl (Recursive) ; 3. Restore Default Permissions: icacls /reset ; ================================================================================================================================= [-HKEY_CLASSES_ROOT\*\shell\runas] [-HKEY_CLASSES_ROOT\Directory\shell\runas] [-HKEY_CLASSES_ROOT\*\shell\322takeown] [-HKEY_CLASSES_ROOT\Directory\shell\322takeown] [-HKEY_CLASSES_ROOT\Drive\shell\322takeown] [-HKEY_CLASSES_ROOT\*\shell\322clearown] [-HKEY_CLASSES_ROOT\Directory\shell\322clearown] [-HKEY_CLASSES_ROOT\Drive\shell\322clearown] [-HKEY_CLASSES_ROOT\*\shell\322freeown] [-HKEY_CLASSES_ROOT\Directory\shell\322freeown] [-HKEY_CLASSES_ROOT\Drive\shell\322freeown] ; Cleanup older versions and concurrent scripts [-HKEY_CLASSES_ROOT\*\shell\TakeOwnership] [-HKEY_CLASSES_ROOT\Directory\shell\TakeOwnership] [-HKEY_CLASSES_ROOT\Drive\shell\TakeOwnership] ; Main context-menu entries and idiotproof filters [HKEY_CLASSES_ROOT\*\shell\TakeOwnership] "MuiVerb"="Take Ownership" "SubCommands"="" "HasLUAShield"="" [HKEY_CLASSES_ROOT\Directory\shell\TakeOwnership] "MuiVerb"="Take Ownership" "SubCommands"="" "HasLUAShield"="" "AppliesTo"="NOT (System.ItemPathDisplay:=\"*:\\Users\" OR System.ItemPathDisplay:=\"*:\\ProgramData\" OR System.ItemPathDisplay:=\"*:\\Program Files\" OR System.ItemPathDisplay:=\"*:\\Program Files (x86)\" OR System.ItemPathDisplay:=\"*:\\Windows\" OR System.ItemPathDisplay:=\"*:\\Recovery\" OR System.ItemPathDisplay:=\"*:\\System Volume Information\" OR System.ItemPathDisplay:=\"*:\\$Recycle.Bin\" OR System.ItemPathDisplay:=\"*:\\Boot\" OR System.ItemPathDisplay:=\"*:\\PerfLogs\" OR System.ItemPathDisplay:=\"*:\\MSOCache\")" ;================================================================================================================================== ; 1. Take Ownership (Non-Recursive) ;================================================================================================================================== ; All file types [HKEY_CLASSES_ROOT\*\shell\TakeOwnership\shell\01takeown] "MuiVerb"="Take Ownership" "HasLUAShield"="" [HKEY_CLASSES_ROOT\*\shell\TakeOwnership\shell\01takeown\command] @="powershell -noexit -c \"Start-Process powershell -ArgumentList ('-NoProfile -noexit -c \\\"$myself=[System.Security.Principal.NTAccount]'''+$env:username+'''; $dll0=''[DllImport(\\\\\\\"ntdll.dll\\\\\\\")]public static extern int RtlAdjustPrivilege(ulong a,bool b,bool c,ref bool d);''; $ntdll=Add-Type -Member $dll0 -Name NtDll -PassThru; foreach($i in @(9,17,18)){$null=$ntdll::RtlAdjustPrivilege($i,1,0,[ref]0)}; $dst=''%1''; $acl=Get-Acl -LiteralPath $dst; $acl.SetOwner($myself); $acl.SetAccessRuleProtection($false,$true); $isDir=(Get-Item -LiteralPath $dst) -is [System.IO.DirectoryInfo]; $flags=if($isDir){''ContainerInherit,ObjectInherit''}else{''None''}; $r=New-Object System.Security.AccessControl.FileSystemAccessRule($myself,''FullControl'',$flags,''None'',''Allow''); $acl.SetAccessRule($r); Set-Acl -LiteralPath $dst -AclObject $acl\\\"') -verb RunAs\"" "IsolatedCommand"="powershell -noexit -c \"Start-Process powershell -ArgumentList ('-NoProfile -noexit -c \\\"$myself=[System.Security.Principal.NTAccount]'''+$env:username+'''; $dll0=''[DllImport(\\\\\\\"ntdll.dll\\\\\\\")]public static extern int RtlAdjustPrivilege(ulong a,bool b,bool c,ref bool d);''; $ntdll=Add-Type -Member $dll0 -Name NtDll -PassThru; foreach($i in @(9,17,18)){$null=$ntdll::RtlAdjustPrivilege($i,1,0,[ref]0)}; $dst=''%1''; $acl=Get-Acl -LiteralPath $dst; $acl.SetOwner($myself); $acl.SetAccessRuleProtection($false,$true); $isDir=(Get-Item -LiteralPath $dst) -is [System.IO.DirectoryInfo]; $flags=if($isDir){''ContainerInherit,ObjectInherit''}else{''None''}; $r=New-Object System.Security.AccessControl.FileSystemAccessRule($myself,''FullControl'',$flags,''None'',''Allow''); $acl.SetAccessRule($r); Set-Acl -LiteralPath $dst -AclObject $acl\\\"') -verb RunAs\"" ; Folders [HKEY_CLASSES_ROOT\Directory\shell\TakeOwnership\shell\01takeown] "MuiVerb"="Take Ownership" "HasLUAShield"="" [HKEY_CLASSES_ROOT\Directory\shell\TakeOwnership\shell\01takeown\command] @="powershell -noexit -c \"Start-Process powershell -ArgumentList ('-NoProfile -noexit -c \\\"$myself=[System.Security.Principal.NTAccount]'''+$env:username+'''; $dll0=''[DllImport(\\\\\\\"ntdll.dll\\\\\\\")]public static extern int RtlAdjustPrivilege(ulong a,bool b,bool c,ref bool d);''; $ntdll=Add-Type -Member $dll0 -Name NtDll -PassThru; foreach($i in @(9,17,18)){$null=$ntdll::RtlAdjustPrivilege($i,1,0,[ref]0)}; $dst=''%1''; $acl=Get-Acl -LiteralPath $dst; $acl.SetOwner($myself); $acl.SetAccessRuleProtection($false,$true); $isDir=(Get-Item -LiteralPath $dst) -is [System.IO.DirectoryInfo]; $flags=if($isDir){''ContainerInherit,ObjectInherit''}else{''None''}; $r=New-Object System.Security.AccessControl.FileSystemAccessRule($myself,''FullControl'',$flags,''None'',''Allow''); $acl.SetAccessRule($r); Set-Acl -LiteralPath $dst -AclObject $acl\\\"') -verb RunAs\"" "IsolatedCommand"="powershell -noexit -c \"Start-Process powershell -ArgumentList ('-NoProfile -noexit -c \\\"$myself=[System.Security.Principal.NTAccount]'''+$env:username+'''; $dll0=''[DllImport(\\\\\\\"ntdll.dll\\\\\\\")]public static extern int RtlAdjustPrivilege(ulong a,bool b,bool c,ref bool d);''; $ntdll=Add-Type -Member $dll0 -Name NtDll -PassThru; foreach($i in @(9,17,18)){$null=$ntdll::RtlAdjustPrivilege($i,1,0,[ref]0)}; $dst=''%1''; $acl=Get-Acl -LiteralPath $dst; $acl.SetOwner($myself); $acl.SetAccessRuleProtection($false,$true); $isDir=(Get-Item -LiteralPath $dst) -is [System.IO.DirectoryInfo]; $flags=if($isDir){''ContainerInherit,ObjectInherit''}else{''None''}; $r=New-Object System.Security.AccessControl.FileSystemAccessRule($myself,''FullControl'',$flags,''None'',''Allow''); $acl.SetAccessRule($r); Set-Acl -LiteralPath $dst -AclObject $acl\\\"') -verb RunAs\"" ;================================================================================================================================== ; 2. Take Ownership (Recursive) ;================================================================================================================================== ; Folders [HKEY_CLASSES_ROOT\Directory\shell\TakeOwnership\shell\02takeownrec] "MuiVerb"="Take Ownership (Recursive)" "HasLUAShield"="" [HKEY_CLASSES_ROOT\Directory\shell\TakeOwnership\shell\02takeownrec\command] @="powershell -noexit -c \"Start-Process powershell -ArgumentList ('-NoProfile -noexit -c \\\"$myself=[System.Security.Principal.NTAccount]'''+$env:username+'''; $dll0=''[DllImport(\\\\\\\"ntdll.dll\\\\\\\")]public static extern int RtlAdjustPrivilege(ulong a,bool b,bool c,ref bool d);''; $ntdll=Add-Type -Member $dll0 -Name NtDll -PassThru; foreach($i in @(9,17,18)){$null=$ntdll::RtlAdjustPrivilege($i,1,0,[ref]0)}; $dst=''%1''; $rD=New-Object System.Security.AccessControl.FileSystemAccessRule($myself,''FullControl'',''ContainerInherit,ObjectInherit'',''None'',''Allow''); $rF=New-Object System.Security.AccessControl.FileSystemAccessRule($myself,''FullControl'',''None'',''None'',''Allow''); function s($i){try{$isDir=$i -is [System.IO.DirectoryInfo]; $acl=Get-Acl -LiteralPath $i.FullName; $acl.SetOwner($myself); $acl.SetAccessRuleProtection($false,$true); $acl.SetAccessRule(if($isDir){$rD}else{$rF}); Set-Acl -LiteralPath $i.FullName -AclObject $acl}catch{}}; s (Get-Item -LiteralPath $dst -Force); Get-ChildItem -LiteralPath $dst -Recurse -Force -Attributes !ReparsePoint -ErrorAction SilentlyContinue | ForEach-Object {s $_}\"') -verb RunAs\"" "IsolatedCommand"="powershell -noexit -c \"Start-Process powershell -ArgumentList ('-NoProfile -noexit -c \\\"$myself=[System.Security.Principal.NTAccount]'''+$env:username+'''; $dll0=''[DllImport(\\\\\\\"ntdll.dll\\\\\\\")]public static extern int RtlAdjustPrivilege(ulong a,bool b,bool c,ref bool d);''; $ntdll=Add-Type -Member $dll0 -Name NtDll -PassThru; foreach($i in @(9,17,18)){$null=$ntdll::RtlAdjustPrivilege($i,1,0,[ref]0)}; $dst=''%1''; $rD=New-Object System.Security.AccessControl.FileSystemAccessRule($myself,''FullControl'',''ContainerInherit,ObjectInherit'',''None'',''Allow''); $rF=New-Object System.Security.AccessControl.FileSystemAccessRule($myself,''FullControl'',''None'',''None'',''Allow''); function s($i){try{$isDir=$i -is [System.IO.DirectoryInfo]; $acl=Get-Acl -LiteralPath $i.FullName; $acl.SetOwner($myself); $acl.SetAccessRuleProtection($false,$true); $acl.SetAccessRule(if($isDir){$rD}else{$rF}); Set-Acl -LiteralPath $i.FullName -AclObject $acl}catch{}}; s (Get-Item -LiteralPath $dst -Force); Get-ChildItem -LiteralPath $dst -Recurse -Force -Attributes !ReparsePoint -ErrorAction SilentlyContinue | ForEach-Object {s $_}\"') -verb RunAs\"" ;================================================================================================================================== ; 3. Restore Default Permissions ;================================================================================================================================== ; All file types [HKEY_CLASSES_ROOT\*\shell\TakeOwnership\shell\03restore] "MuiVerb"="Restore Default Permissions" "HasLUAShield"="" [HKEY_CLASSES_ROOT\*\shell\TakeOwnership\shell\03restore\command] @="powershell -noexit -c \"Start-Process powershell -ArgumentList ('-NoProfile -noexit -c \\\"$dll0=''[DllImport(\\\\\\\"ntdll.dll\\\\\\\")]public static extern int RtlAdjustPrivilege(ulong a,bool b,bool c,ref bool d);''; $ntdll=Add-Type -Member $dll0 -Name NtDll -PassThru; foreach($i in @(9,17,18)){$null=$ntdll::RtlAdjustPrivilege($i,1,0,[ref]0)}; $dst=''%1''; $sys=''NT AUTHORITY\\SYSTEM''; $isDir=(Get-Item -LiteralPath $dst) -is [System.IO.DirectoryInfo]; if($isDir){ & icacls.exe $dst /setowner $sys /T /C /Q; & icacls.exe $dst /reset /T /C /Q } else { & icacls.exe $dst /setowner $sys /C /Q; & icacls.exe $dst /reset /C /Q }\\\"') -verb RunAs\"" "IsolatedCommand"="powershell -noexit -c \"Start-Process powershell -ArgumentList ('-NoProfile -noexit -c \\\"$dll0=''[DllImport(\\\\\\\"ntdll.dll\\\\\\\")]public static extern int RtlAdjustPrivilege(ulong a,bool b,bool c,ref bool d);''; $ntdll=Add-Type -Member $dll0 -Name NtDll -PassThru; foreach($i in @(9,17,18)){$null=$ntdll::RtlAdjustPrivilege($i,1,0,[ref]0)}; $dst=''%1''; $sys=''NT AUTHORITY\\SYSTEM''; $isDir=(Get-Item -LiteralPath $dst) -is [System.IO.DirectoryInfo]; if($isDir){ & icacls.exe $dst /setowner $sys /T /C /Q; & icacls.exe $dst /reset /T /C /Q } else { & icacls.exe $dst /setowner $sys /C /Q; & icacls.exe $dst /reset /C /Q }\\\"') -verb RunAs\"" ; Folders [HKEY_CLASSES_ROOT\Directory\shell\TakeOwnership\shell\03restore] "MuiVerb"="Restore Default Permissions" "HasLUAShield"="" [HKEY_CLASSES_ROOT\Directory\shell\TakeOwnership\shell\03restore\command] @="powershell -noexit -c \"Start-Process powershell -ArgumentList ('-NoProfile -noexit -c \\\"$dll0=''[DllImport(\\\\\\\"ntdll.dll\\\\\\\")]public static extern int RtlAdjustPrivilege(ulong a,bool b,bool c,ref bool d);''; $ntdll=Add-Type -Member $dll0 -Name NtDll -PassThru; foreach($i in @(9,17,18)){$null=$ntdll::RtlAdjustPrivilege($i,1,0,[ref]0)}; $dst=''%1''; $sys=''NT AUTHORITY\\SYSTEM''; $isDir=(Get-Item -LiteralPath $dst) -is [System.IO.DirectoryInfo]; if($isDir){ & icacls.exe $dst /setowner $sys /T /C /Q; & icacls.exe $dst /reset /T /C /Q } else { & icacls.exe $dst /setowner $sys /C /Q; & icacls.exe $dst /reset /C /Q }\\\"') -verb RunAs\"" "IsolatedCommand"="powershell -noexit -c \"Start-Process powershell -ArgumentList ('-NoProfile -noexit -c \\\"$dll0=''[DllImport(\\\\\\\"ntdll.dll\\\\\\\")]public static extern int RtlAdjustPrivilege(ulong a,bool b,bool c,ref bool d);''; $ntdll=Add-Type -Member $dll0 -Name NtDll -PassThru; foreach($i in @(9,17,18)){$null=$ntdll::RtlAdjustPrivilege($i,1,0,[ref]0)}; $dst=''%1''; $sys=''NT AUTHORITY\\SYSTEM''; $isDir=(Get-Item -LiteralPath $dst) -is [System.IO.DirectoryInfo]; if($isDir){ & icacls.exe $dst /setowner $sys /T /C /Q; & icacls.exe $dst /reset /T /C /Q } else { & icacls.exe $dst /setowner $sys /C /Q; & icacls.exe $dst /reset /C /Q }\\\"') -verb RunAs\"" '@ # Сохранение в формате UTF-16 LE (Unicode), который 100% корректно понимается утилитой reg.exe [IO.File]::WriteAllText($regPath, $regContent, [System.Text.Encoding]::Unicode) # 3. Применение reg-файла Write-Host "Importing Registry keys..." -ForegroundColor Cyan $process = Start-Process "reg.exe" -ArgumentList "import `"$regPath`"" -Wait -NoNewWindow -PassThru if ($process.ExitCode -eq 0) { Write-Host "Registry imported successfully!" -ForegroundColor Green } else { Write-Host "Warning: Registry import returned exit code $($process.ExitCode)." -ForegroundColor Red } # 4. Заключительная подготовка и уборка Write-Host "Cleaning up temporary files..." -ForegroundColor Cyan Remove-Item -Path $regPath -Force -ErrorAction SilentlyContinue Write-Host "Done! The advanced 'Take Ownership' context menu is now installed." -ForegroundColor Green Start-Sleep -Seconds 3 pause