Question Is it possible to use a script or a batch file to disable/enable a specific peripheral ?

Status
Not open for further replies.

Greywolf74

Distinguished
Jan 27, 2015
37
2
18,535
Is it possible to use a script or a batch file to disable/enable a specific peripheral? I have a gamepad that I would like to be able to toggle on/off. It causes Windows not to be able to turn the monitors off after X amount of time and I dont want to have to physically get under my desk and unplug it every time Im done playing for the day. TIA
 
Is it possible to use a script or a batch file to disable/enable a specific peripheral?
You can use devcon utility to disable/enable devices.


 
Just for future reference in case someone else is trying to figure out how to do this heres the powershell code. Youll have to change your hardware ID to match your device(s) that you want to enable/disable but this will get you in the ballpark. In my case I was trying to disable a Razer Tartarus gamepad because my monitors wouldnt turn off when it was plugged in. I discovered that I had to not only disable the device under "keyboards" in device manager but I also had to disable the hardware ID for it that showed up under "Human Interface Devices" as well.

# Self-elevating PowerShell script to toggle the enabled state of a device
# Check to see if we are currently running "as Administrator"
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
# We are not running "as Administrator" - so relaunch as administrator
# Create a new process object that starts PowerShell
$newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell";
# Specify the current script path and name as a parameter
$newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Definition + "'"
# Indicate that the process should be run as administrator
$newProcess.Verb = "runas";
# Start the new process
[System.Diagnostics.Process]::Start($newProcess);
# Exit from the current, unelevated script
exit
}

# Function to toggle the device state by Hardware ID
function Toggle-DeviceStateByHardwareID {
param (
[string]$HardwareID
)

$device = Get-PnpDevice | Where-Object { $_.HardwareID -like "*$HardwareID*" }

if ($device -eq $null) {
Write-Host "No device found with Hardware ID: $HardwareID."
return
}

foreach ($dev in $device) {
if ($dev.Status -eq "OK") {
# Device is enabled, so disable it
$dev | Disable-PnpDevice -Confirm:$false
Write-Host "Device '$($dev.FriendlyName)' with Hardware ID: $HardwareID is now disabled."
} elseif ($dev.Status -eq "Error") {
# Device is disabled, so enable it
$dev | Enable-PnpDevice -Confirm:$false
Write-Host "Device '$($dev.FriendlyName)' with Hardware ID: $HardwareID is now enabled."
} else {
Write-Host "Device '$($dev.FriendlyName)' with Hardware ID: $HardwareID is in an unknown state."
}
}
}

# Toggle the devices using their Hardware IDs
Toggle-DeviceStateByHardwareID -HardwareID "USB\VID_1532&PID_0208&MI_01&JS_02"
Toggle-DeviceStateByHardwareID -HardwareID "HID\VID_1532&PID_0208&REV_0200&MI_02"
 
Last edited:
  • Like
Reactions: Grobe
Steam has controller support, for many many controllers, and has a setting that shuts off controllers after 15min of inactivity, did that not work for you?!
I was not aware of this and you were the first person to mention it. I have set the idle timeout and Ill see if that works. If not I know my script works :)

Thank you!
 
So just an update about the Steam controller settings. It does not work. I set it to time out after 10 minutes and the screen is set to turn off after 15 minutes and the monitors still do not shut off. Using the script to disable to device does work. just FYSA
 
Status
Not open for further replies.