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

3

3

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.

Aravind .KEN

Posted 2016-12-17T11:43:53.163

Reputation: 99

1

Use a vbscript to check your connection status, you'll have to insert your adapter name.

– LotPings – 2016-12-17T12:16:59.413

Welcome to Super User! Please note that https://superuser.com is not a free script/code writing service. If you tell us what you have tried so far (include the scripts/code you are already using) and where you are stuck then we can try to help with specific problems. You should also read How do I ask a good question?.

– DavidPostill – 2016-12-17T15:19:47.093

Changing your question after you've received answers is inappropriate, as it invalidates the answers you've received. It can even make those answers wrong, and adversely affect the reputation of those who answered. If you now have a new or additional question, create a new post and ask it there; you can link back to this one if needed for reference. – DavidPostill – 2016-12-17T15:25:55.257

@DavidPostill You have mistaken me! I dont change the question after i got the answer! First of all , i dont change i Added! Second, I added because the answers which i got were not the answer that i expected! So I MADE THE QUESTION MORE CLEAR, WHAT I WANT! Anyway sorry, I should have done that earlier. – Aravind .KEN – 2016-12-17T15:39:47.597

You did change it. You added extra requirements. – DavidPostill – 2016-12-17T15:41:12.400

@DavidPostill Yeah, I did! because the answer which got was not that i wanted so i added! As i said " Anyway sorry, I should have done that earlier." – Aravind .KEN – 2016-12-17T15:43:15.437

Answers

3

try this:

netsh wlan show interfaces | Findstr /c:"Signal" && Echo Online || Echo Offline

Aravind .KEN

Posted 2016-12-17T11:43:53.163

Reputation: 99

3

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

n8te

Posted 2016-12-17T11:43:53.163

Reputation: 6 068

well i got :NetConnectionStatus 7 – Aravind .KEN – 2016-12-17T12:25:18.847

Bro, I only YES or No answer. – Aravind .KEN – 2016-12-17T12:25:56.837

*Edited to include the result of no Wifi connection – n8te – 2016-12-17T12:30:03.880

I just need Yes or No answer form it. Because i wanna run a loop depending upon the answers i get – Aravind .KEN – 2016-12-17T12:31:14.353

Yeah, I wrote that answer before you edited your question with that requirement. – n8te – 2016-12-17T12:32:29.073

But How i will my program know it? because i cant use if loop on it. Using ur command line, I should manually run a loop in batch file. I wanna made it automatic. Did u get want i am trying to say?: – Aravind .KEN – 2016-12-17T12:40:31.210

Yes, I understand now what you're trying to do. Like I said, my answer came before you amended your original question with that additional clarification. If you want I can delete the answer since it no longer matches your question. But programmatically it's not that hard to run an if loop off the results of my command. – n8te – 2016-12-17T12:53:03.540

Thank bro, for ur time, I hope you could get me an answer. – Aravind .KEN – 2016-12-17T13:58:15.980

3

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)

LotPings

Posted 2016-12-17T11:43:53.163

Reputation: 6 150

+1 for pointing out my error and giving OP the full code he needs – n8te – 2016-12-17T13:09:30.137

@n8te Thats no error just not neccessary. And kudos to you for the solution +1. – LotPings – 2016-12-17T13:12:53.903

well the batch file crashes for me – Aravind .KEN – 2016-12-17T13:17:14.397

Could you elaborate on that? Just crashes isn't helpful. – LotPings – 2016-12-17T13:25:07.557

When i copy the above code and paste it in my notepad and saved it as test.bat. ! when i run it simply closes in a sec. – Aravind .KEN – 2016-12-17T13:57:22.790

Well new code is best tested inside an open cmd window. I guess you simply clicked on the file. I'll add a pause at the end so you can see the output. But how would you have expected to process the answer with a loop? – LotPings – 2016-12-17T14:01:24.077

Errors that i get when i paste in cmd :C:\WINDOWS\system32>@Echo off For /f %%A in ( %%A was unexpected at this time. 'WMIC path WIN32_NetworkAdapter where (NetConnectionID="Wi-Fi") get NetConnectionStatus' ''WMIC' is not recognized as an internal or external command, operable program or batch file. ) Do If %%A Equ 2 (Echo yes) Else (Echo No) Pause Could upload me a batch file? – Aravind .KEN – 2016-12-17T14:08:47.447

@Aravind.KEN You have to save this as a file with the extension .bat or .cmd. Or when you want to execute directly in the cmd line change the %%Ato only %A – LotPings – 2016-12-17T14:12:46.310

when I want to execute directly in the cmd, i get errors:

C:\WINDOWS\system32>For /f %A in ('WMIC path WIN32_NetworkAdapter where (NetConnectionID="Wi-Fi") get NetConnectionStatus') Do If %A Equ 2 (Echo yes) Else (Echo No)
get was unexpected at this time.

C:\WINDOWS\system32>        (i also the same problem as before even i saved it as .bat as you told)
 – Aravind .KEN  – 2016-12-17T14:35:17.180

@Aravind.KEN I have no wlan adapter so I removed the where clause and didn't remember to escape the inner parentheses. See updated answer. We should move to chat for any other question. – LotPings – 2016-12-17T14:41:27.617

Let us continue this discussion in chat.

– Aravind .KEN – 2016-12-17T14:48:14.103

Let us continue this discussion in chat.

– Aravind .KEN – 2016-12-18T13:05:30.503

1

You can enter ipconfig in cmd, then if you connected, Wireless LAN adapter Wi-Fi should be assigned IP. If you are still disconnected the Media State should be Media disconnected

enter image description here

Milad Sobhkhiz

Posted 2016-12-17T11:43:53.163

Reputation: 251

1I just need Yes or No answer form it. Because i wanna run a loop depending upon the answers i get – Aravind .KEN – 2016-12-17T12:30:25.233