Advertisement
aveyo

ToggleGameBar-properly

Nov 29th, 2017 (edited)
14,023
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 3.79 KB | None | 1 0
  1. @(echo off% <#%) &title ToggleGamebar - properly [updated 2022.08.16]
  2. set "0=%~f0"&set 1=%*&powershell -nop -win 1 -c iex ([io.file]::ReadAllText($env:0)) &exit /b ||#>)[1]
  3. ## wrap script
  4. sp 'HKCU:\Volatile Environment' 'ToggleGamebar' @'
  5.  
  6. ## status for GamebarPresenceWriter
  7. $key='HKLM:\SOFTWARE\Microsoft\WindowsRuntime\Server\Windows.Gaming.GameBar.Internal.PresenceWriterServer'
  8. if ((gp $key ExePath -ea 0).ExePath -like "*systray.exe*") {$Y=6;$N=7;$A='Enable';$S='OFF'}else{$Y=7;$N=6;$A='Disable';$S='ON'}
  9. ## dialog prompt with Yes, No, Cancel (6,7,2)
  10. if ($env:1 -ne 6 -and $env:1 -ne 7) {
  11.   $choice=(new-object -ComObject Wscript.Shell).Popup($A + ' Gamebar?', 0, 'Gamebar is: ' + $S, 0x1033)
  12.   if ($choice -eq 2) {break} elseif ($choice -eq 6) {$env:1=$Y} else {$env:1=$N}
  13. }
  14.  
  15. ## toggle Gamebar
  16. sp 'HKCU:\Software\Microsoft\Windows\CurrentVersion\GameDVR' AppCaptureEnabled ($env:1-6) -Type Dword -Force -ea 0
  17.  
  18. ## relaunch as Admin passing cmdline args
  19. $script='-nop -c & {$env:1='+$env:1+';iex((gp Registry::HKEY_Users\S-1-5-21*\Volatile* ToggleGamebar -ea 0)[0].ToggleGamebar)}'
  20. $null=fltmc;if($LASTEXITCODE -gt 0){start powershell -args $script -verb runas -win 1; break}
  21.  
  22. function reg_own { param ( $key, $recurse='', $user='S-1-5-32-544', $owner='', $acc='Allow', $perm='FullControl', [switch]$list )
  23.   $D1=[uri].module.gettype('System.Diagnostics.Process')."GetM`ember"('SetPrivilege',42)[0]; $u=$user; $o=$owner; $p=524288
  24.   'SeSecurityPrivilege','SeTakeOwnershipPrivilege','SeBackupPrivilege','SeRestorePrivilege' |% {$D1.Invoke($null, @("$_",2))}
  25.   $reg=$key-split':?\\',2; $key=$reg-join'\'; $HK=gi -lit Registry::$($reg[0]) -force; $re=$recurse; $in=(1,0)[$re-eq'Inherit']
  26.   $own=$o-eq''; if($own){$o=$u}; $sid=[Security.Principal.SecurityIdentifier]; $w='S-1-1-0',$u,$o |% {new-object $sid($_)}
  27.   $r=($w[0],$p,1,0,0),($w[1],$perm,1,0,$acc) |% {new-object Security.AccessControl.RegistryAccessRule($_)}; function _own($k,$l) {
  28.   $t=$HK.OpenSubKey($k,2,'TakeOwnership'); if($t) { try {$n=$t.GetAccessControl(4)} catch {$n=$HK.GetAccessControl(4)}
  29.   $u=$n.GetOwner($sid); if($own-and $u) {$w[2]=$u}; $n.SetOwner($w[0]); $t.SetAccessControl($n); $d=$HK.GetAccessControl(2)
  30.   $c=$HK.OpenSubKey($k,2,'ChangePermissions'); $b=$c.GetAccessControl(2); $d.RemoveAccessRuleAll($r[1]); $d.ResetAccessRule($r[0])
  31.   $c.SetAccessControl($d); if($re-ne'') {$sk=$HK.OpenSubKey($k).GetSubKeyNames(); foreach($i in $sk) {_own "$k\$i" $false}}
  32.   if($re-ne'') {$b.SetAccessRuleProtection($in,1)}; $b.ResetAccessRule($r[1]); if($re-eq'Delete') {$b.RemoveAccessRuleAll($r[1])}
  33.   $c.SetAccessControl($b); $b,$n |% {$_.SetOwner($w[2])}; $t.SetAccessControl($n)}; if($l) {return $b|fl} }; _own $reg[1] $list
  34. } # :reg_own: lean & mean snippet by AveYo
  35.  
  36. $GameBarPresenceWriter=("$env:systemroot\System32\GameBarPresenceWriter.exe","$env:systemroot\System32\systray.exe")[$env:1 -gt 6]
  37. $key='HKLM:\SOFTWARE\Microsoft\WindowsRuntime\Server\Windows.Gaming.GameBar.Internal.PresenceWriterServer'
  38. if (test-path $key) {
  39.   reg_own $key -user S-1-1-0
  40.   sp $key ExePath $GameBarPresenceWriter -Force -ea 0
  41.   reg_own $key -user S-1-1-0 -recurse Delete
  42. }
  43.  
  44. $GameBarFTServer=('GameBarFTServer.exe','systray.exe')[$env:1 -gt 6]
  45. $app=(dir 'HKLM:\SOFTWARE\Classes\PackagedCom\ClassIndex\{FD06603A-2BDF-4BB1-B7DF-5DC68F353601}').PSChildName
  46. $key="HKLM:\SOFTWARE\Classes\PackagedCom\Package\$app\Server\0"
  47. if (test-path $key) {
  48.   reg_own $key -user S-1-1-0
  49.   sp $key Executable $GameBarFTServer -Force -ea 0
  50.   reg_own $key -user S-1-1-0 -recurse Delete
  51. }
  52.  
  53. ## stop processes
  54. 'GameBar','GamebarPresenceWriter','GameBarFTServer' |foreach {kill -Force -Name $_ -ea 0}
  55.  
  56. ## execute script
  57. '@ -Force -ea 0; iex((gp Registry::HKEY_Users\S-1-5-21*\Volatile* ToggleGamebar -ea 0)[0].ToggleGamebar)
  58. #-_-# hybrid script, can be pasted directly into powershell console
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement