Thursday, February 4, 2010

Shortcut for enabling/disabling wireless device (or any device)

Most laptops these days have a switch or button to toggle the wireless device.  Turning off wireless is a good way to save battery, and having a shortcut to turn it off is nice.  The switch on the previous laptop I had would completely turn off the device.  However, the new laptops we got at work seem to only turn off the antennae.  Power-wise this is fine, but Windows continually opens one of the bubble notifications to tell me that the wireless isn't connected.

Nothing is broken, but it started getting really annoying to have to dismiss that notification a couple times an hour.  Extra annoying is the fact that Windows provides a checkbox that looks like it should turn that behavior off, but even with that unchecked it still bugs me.


The only way I could find to make it stop bugging me was to turn off the wireless device itself.  Having to dig into the network devices every time I want to toggle it would have been quite annoying though, so I decided to try and write a script to do it.

The first step was to find a way to toggle devices from the command line.  Turns out Microsoft provides the DevCon tool for doing just that.  Install that somewhere in your PATH (I used C:\bin).

Next step is to get the ID of the device you want to toggle.  You can use devcon hwids to get the list of all IDs and then search that output for your device, or use Device Manager.  From Device Manager, find your wireless device, get its properties, then go to the Details tab and select to view the Device ID:


You only need a subset of that giant ID.  For me the full ID displayed is:
PCI\VEN_8086&DEV_4236&SUBSYS_10118086&REV_00\4&318470AD&0&00E1
The actual value needed is the DEV_# value:  DEV_4236.

Now we can use devcon to turn the device on or off and query its status:


Originally I wrote separate bat files for enabling and disabling the device.  However, what I really wanted was a way to detect the current status and toggle it.  I wasn't sure how to do it in a bat file, but was able to get a simple powershell script working, which I saved at C:\bin\wirelessToggle.ps1:

$devid = "*DEV_4236"

$status = devcon status $devid | Select-String "running"

if($status -eq $null)
{
  write-host "Enabling wireless"
  devcon enable $devid
}
else
{
  write-host "Disabling wireless"
  devcon disable $devid
}

All that's left is to have a shortcut to run that.  I simply created a shortcut using for powershell that's passed in the script:


Special thanks to Molly on superuser for pointing me at DevCon and that post on how to use it.

Script/shortcut to toggle wireless (or any other) device in Windows.

2 comments:

  1. Seems excessive. Try windowskey + x.

    ReplyDelete
  2. I had the same issue.. wanted to disable the wireless due to health concerns... there is however a simple way to do enable/disable your wirelss adapter in device manager... go to network connections ( click on change adapter setting under network and sharing center in control panel) and right click on your wireless connection and select "create shortcut".. the shortcut would be placed on desktop.. right click the shortcut created on desktop and click on "enable" or "disable" the adapter. works on my win7 laptop.. hope it is still relevant to you.

    ReplyDelete