Skip to content

Instantly share code, notes, and snippets.

@GleasonK
Created February 7, 2022 16:41
Show Gist options
  • Save GleasonK/822457e90a7fe19a30cba59d9942de20 to your computer and use it in GitHub Desktop.
Save GleasonK/822457e90a7fe19a30cba59d9942de20 to your computer and use it in GitHub Desktop.
Useful Script for Surface Book 2 to toggle off GTX when attempting to detach the screen.
# Useful script for detaching the screen of Surface Book.
# Run if detach light blinks red indicating that it cannot be detached
# since applications are preventing the detach.
#
# Option 1: Copy-paste into powershell window.
#
# Option 2: Run as PowerShell script (requires execution policy change):
# https://superuser.com/questions/106360/how-to-enable-execution-of-powershell-scripts
#
Try {
$GtxId = (Get-PnpDevice -FriendlyName "*GTX*").InstanceId
echo " DeviceID = $GtxId"
echo "Disabling GTX."
Disable-PnpDevice -Confirm:$false -InstanceId $GtxId
Read-Host -Prompt "Safe to detach. Hit enter to re-enable."
echo "Enabling GTX."
Enable-PnpDevice -Confirm:$false -InstanceId $GtxId
} Catch {
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
echo "Try Run as Admin."
echo "ERROR: $ErrorMessage"
echo "ERROR: $FailedItem"
} Finally {
Read-Host -Prompt "Press any key to quit"
}
@jonaskuske
Copy link

jonaskuske commented Oct 13, 2022

Super handy script, thanks!!

Adapted it a bit, so that it automatically asks for admin permissions if necessary and also automatically toggles between enabling and disabling the GPU based on the device state, so the command doesn't need to be interactive, and you don't need to keep the PowerShell window open while in clipboard mode:

if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))  
{  
  $arguments = "& '" +$myinvocation.mycommand.definition + "'"
  Start-Process powershell -Verb runAs -ArgumentList $arguments
  Break
}

Try {
	$Gtx = Get-PnpDevice -FriendlyName "*GTX*"

  if ($Gtx.Status -eq "OK") {
	  echo "Disabling GTX."
	  Disable-PnpDevice -Confirm:$false -InstanceId $Gtx.InstanceId
  } else {
  	echo "Reenabling GTX."
	  Enable-PnpDevice -Confirm:$false -InstanceId $Gtx.InstanceId
  }
} Catch {
	$ErrorMessage = $_.Exception.Message
	$FailedItem = $_.Exception.ItemName
	echo "ERROR: $ErrorMessage"
	echo "ERROR: $FailedItem" 
}

@DavidGitter
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment