Using cmd/Batch, how to determine whether or not you are connected to a WiFi network?

Using netsh wlan connect name="your network_name" you can request to connect to a WiFi network, but can't be sure whether or not you are connected.

So, in CMD/Batch, what would be a command line to check whether or not I am connected to a WiFi network? (The WiFi network may, or may not, have network access.)[It should also work for Mobile hotspots too]

If Connected, It should display YES,

If NOT Connected, It should display NO,

because I want to run a loop depending upon the results I get.


For an alternative method to Milad's answer for checking whether your Windows PC is currently connected to a Wifi network, do the following:

  • Open a command prompt
  • Enter the following command:

WMIC /node: ”PutYourPCNameHere” path WIN32_NetworkAdapter where (NetConnectionID="Wi-Fi") get NetConnectionStatus

  • If you are currently connected to Wifi it will return the following result:

    NetConnectionStatus 2

  • If you are not currently connected it will return this result:

    NetConnectionStatus 7


You don't need to reference the actual Pc explicitly. Store this in a file with the extension .bat or .cmd

@Echo off
For /f "usebackq" %%A in (
  `WMIC path WIN32_NetworkAdapter where 'NetConnectionID="Wi-Fi"' get NetConnectionStatus`
) Do If %%A Equ 2 (Echo yes) Else (Echo No)
Pause

This has to be entered in a cmd window (Sorry for the misunderstanding)

 For /f %A in ('WMIC path WIN32_NetworkAdapter where (NetConnectionID="Wi-Fi"^) get NetConnectionStatus') Do If %A Equ 2 (Echo yes) Else (Echo No)