Using WMIC to Enable and Disable The Wireless Network Adapter
The Utility
WMIC is the Windows Management Interface Command-line Utility a command-line interface to the WMI libraries. Here I'm going to focus on enabling and disabling the wireless interface.Note: Since these are DOS commands they are case-insensitive (even inside quotes) so the mixed cases are only used to make it easier to read.
The Library
To enable and disable the interface you use the Win32_NetworkAdapter class.The Interface
To get the interface name you can list the interfaces:wmic path Win32_NetworkAdapterBut this will dump a mess of lines so here we'll assume you just know it's Wireless Network Connection (if you click on the wireless icon on the TaskBar you'll see that this is the name above the list of visible access points). This value is used for the NetConnectionID in the command.
To Dump Interface Information
If you want to dump all the Information:wmic path Win32_NetworkAdapter where NetConnectionID="Wireless Network Connection"If you want to get a single item (e.g. MAC Address):
wmic path Win32_NetworkAdapter where NetConnectionID="Wireless Network Connection" get MacAddress
To Enable the Interface
Call the enable method:wmic path Win32_NetworkAdapter where NetConnectionID="Wireless Network Connection" call enable
To Disable The Interface
Call the disable method:wmic path Win32_NetworkAdapter where NetConnectionID="Wireless Network Connection" call disable
Errors
Call Errors
A successful change of the interface:Executing (\\IGOR\root\cimv2:Win32_NetworkAdapter.DeviceID="11")->enable()An unsuccessful one will have a non-zero ReturnValue and message.
Method execution successful.
Out Parameters:
instance of __PARAMETERS
{
ReturnValue = 0;
};
Interface Name Errors
If you use an invalid name you'll see:No Instance(s) Available.
Maddening Variations
Cygwin
For unknown reasons, if you use a single pair of quotes in the command, you get the error:Node - IGORWhere IGOR is the name of the computer. To fix this you need to nest another pair of quotation marks inside the first. For example:
ERROR:
Description = Invalid query
wmic path win32_networkadapter where NetConnectionID="'Wireless networK connection'" get Macaddress
Non-Interactive SSH
If you pass in the command as a string to an SSH Connection (e.g. in Paramiko), you need to put quotes around the whole thing and escape the nested quotation marks:'wmic path win32_networkadapter where NetConnectionID="\'Wireless networK connection\'" get Macaddress'