Can a batch file be written to check status of Wifi adapter?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pete_agreatguy
Posts: 7
Joined: 31 May 2014 04:35
Location: UK
Contact:

Can a batch file be written to check status of Wifi adapter?

#1 Post by pete_agreatguy » 31 May 2014 04:44

Hi all,

Is it possible to have a batch file check the status (Enabled/Disabled) of a wireless adapter and as an outcome have it change the adapters setting to the opposite of it's current setting?

i.e. If set as ENABLED, it will disable it as an outcome.
If set as DISABLED, it will enable it as an outcome.

Thanks as this would be extremely useful to overcome a recent problem with Windows 7 not recognising my dads laptop FN key (aeroplane mode).


Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Can a batch file be written to check status of Wifi adap

#3 Post by Squashman » 31 May 2014 10:52

foxidrive wrote:See if you can find a script here:

https://www.google.com.au/search?hl=en& ... apter+wmic

I don't think your Aussie Google Foo is as good as our USA Google Foo! :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Can a batch file be written to check status of Wifi adap

#4 Post by foxidrive » 31 May 2014 23:53

hehe Google doesn't give us a choice - you get the au one by default.

pete_agreatguy
Posts: 7
Joined: 31 May 2014 04:35
Location: UK
Contact:

Re: Can a batch file be written to check status of Wifi adap

#5 Post by pete_agreatguy » 01 Jun 2014 08:18

Hi all,

I've managed to program 2 batch files (one enables and the other disables):
ENABLE WIFI =

Code: Select all

@echo off
echo Please wait ... enabling wireless adapter ...
netsh interface set interface "Wireless Network Connection" ENABLE
echo Your wireless network adapter should now be enabled.
timeout/t 10

DISABLE WIFI =

Code: Select all

@echo off
echo Please wait ... disabling wireless adapter ...
netsh interface set interface "Wireless Network Connection" DISABLE
echo Your wireless network adapter should now be disabled.
timeout/t 10

These both work perfectly on my Windows 7 laptop. 8)

HOWEVER ... I'd like to code a single batch file which first checks the connect status of the wireless adapter and then through a IF statement sets the opposite outcome to the current status.

I'm not entirely sure how to code this though. :oops:

I know ... netsh interface show interface name="Wireless Network Connection" ... provides the connection status with "Connect State".

What is the syntax I should use to lookup this connect state value?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Can a batch file be written to check status of Wifi adap

#6 Post by foxidrive » 01 Jun 2014 08:29

This should do it...

Code: Select all

@echo off
echo Please wait ... toggling wireless adapter ...
set "toggle=ENABLE"
netsh interface show interface name="Wireless Network Connection" 2>&1 |find /i "Connect state:" | /i find " Connected" >nul && set "toggle=DISABLE"
netsh interface set interface "Wireless Network Connection" %toggle%
echo Process completed
timeout /t 10

pete_agreatguy
Posts: 7
Joined: 31 May 2014 04:35
Location: UK
Contact:

Re: Can a batch file be written to check status of Wifi adap

#7 Post by pete_agreatguy » 01 Jun 2014 09:11

Hi foxidrive,

Thank you for your reply. The code you provided did not work. I edited it slightly ( see below) but it only disables the wifi (if enabled). If disabled it just stays disabled.

Code: Select all

@echo off
echo Please wait ... toggling wireless adapter ...
set "toggle=ENABLE"
netsh interface show interface name="Wireless Network Connection" 2>&1 | find /i "Connected" >nul && set "toggle=DISABLE"
netsh interface set interface "Wireless Network Connection" %toggle%
echo Process completed
timeout /t 10

pete_agreatguy
Posts: 7
Joined: 31 May 2014 04:35
Location: UK
Contact:

Re: Can a batch file be written to check status of Wifi adap

#8 Post by pete_agreatguy » 01 Jun 2014 09:20

Got it working 8) See below ...

Code: Select all

@echo off
echo Please wait ... toggling wireless adapter ...
netsh interface show interface name="Wireless Network Connection" | find /i "Connected" >nul && set "toggle=DISABLE"
netsh interface show interface name="Wireless Network Connection" | find /i "Disconnected" >nul && set "toggle=ENABLE"
netsh interface set interface "Wireless Network Connection" %toggle%
echo Process completed
timeout /t 10

Thanks for all your help as much appreciated :)

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Can a batch file be written to check status of Wifi adap

#9 Post by foxidrive » 01 Jun 2014 09:31

I see why my version failed - that's fixed by placing a space in front of " Connected" like so.
Removing the /i case insensitive switch would have fixed it too.

For the same reason your first wmic line will ALWAYS set the variable to DISABLE :)
and it works because the second wmic line will set it to ENABLE when it's needed.

Using your code, this will also work:

Code: Select all

@echo off
echo Please wait ... toggling wireless adapter ...
set "toggle=DISABLE"
netsh interface show interface name="Wireless Network Connection" | find /i "Disconnected" >nul && set "toggle=ENABLE"
netsh interface set interface "Wireless Network Connection" %toggle%
echo Process completed
timeout /t 10

pete_agreatguy
Posts: 7
Joined: 31 May 2014 04:35
Location: UK
Contact:

Re: Can a batch file be written to check status of Wifi adap

#10 Post by pete_agreatguy » 01 Jun 2014 10:16

I'll use your code as it seems more tidier :)
Thanks again.

pete_agreatguy
Posts: 7
Joined: 31 May 2014 04:35
Location: UK
Contact:

Re: Can a batch file be written to check status of Wifi adap

#11 Post by pete_agreatguy » 07 Jun 2014 17:20

Hi all,

The other day before passing the laptop onto my dad, I enabled UAC (user account control).

Upon demonstrating it to my dad when he came round today to pickup his new laptop; the command prompt window provided the error "An interface with this name is not registered with the router.".

I checked the name of the device and compared it to the device name within the batch file. They were both the same.

I disabled UAC and it worked.

For now I've asked my dad to keep the UAC turned off.

Do you know why this would provide this error? If so is there a way around it? No problem if not as he can cope without UAC turned on.

pete_agreatguy
Posts: 7
Joined: 31 May 2014 04:35
Location: UK
Contact:

Re: Can a batch file be written to check status of Wifi adap

#12 Post by pete_agreatguy » 07 Jun 2014 17:43

Found the answer ... sort of.

http://support.microsoft.com/kb/929858c

Is there any "code" which I can use to run the command as the admin account?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Can a batch file be written to check status of Wifi adap

#13 Post by foxidrive » 07 Jun 2014 20:18

The usual method is to right click the icon and select run as admin.

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Can a batch file be written to check status of Wifi adap

#14 Post by Dos_Probie » 08 Jun 2014 18:13

On Windows 7 or 8 I can just enable or toggle back on my Wi-Fi with this script which will scan all available .xml files
present then reconnect if found.

Code: Select all

for /f "tokens=*" %%a in ('dir/b/s "%ProgramData%\Microsoft\Wlansvc\Profiles\Interfaces"') do netsh wlan add profile filename="%%a">nul

Post Reply