Windows – Turn on Wi-Fi via command line

batch filecommand linewindows 10wireless-networking

I have a batch file/script that disconnects from the current WiFi network, connects to a specific WiFi network then sets a static IP address, and it works on Windows 7 but not on Windows 10 if the Wi-Fi is "turned off". How do I "turn on" the Wi-Fi via the command line (or batch file/script since I can normally figure out the syntax of one from the other)?

If it helps, the control panel -> Network and Internet -> Network Connections, Wi-Fi is enabled, but has a red X on it and netsh wlan connect name=%ltName% does not work (where ltName is set to the correct Wi-Fi profile name and it is a known network). Also, the Settings, Wi-Fi slider button at the top of the page says off.

Also, the script is run as Administrator and running Windows 10 Pro version 1607.

Script:

:: disconnect from the current network
netsh wlan disconnect

set ltName=insertNameHere

:: now connect to the wifi
netsh wlan connect name=%ltName%
netsh interface ip set address "Wi-Fi" static xxx.xxx.xxx.xxx 255.255.255.0

::pause

Best Answer

As far as I know and could find after searching extensively, there is no way to do this directly. Turning off Wi-Fi is something similar to airplane mode, and there is no way to turn off airplane mode with commands either. An extremely ugly hack would be to write a script that simulates keystrokes to do the task. In vbscript, this would look like (tested, you may need to change the timeouts)

set shell = CreateObject("WScript.Shell")
shell.run"ms-settings:network-wifi"
WScript.Sleep 2500
shell.SendKeys" "
WScript.Sleep 1500
shell.SendKeys"%{F4}"

Then you can call the script as

cscript toggle-wifi.vbs

You could also work with ms-availablenetworks: which takes less time to open

Related Question