Switch between ethernet and wifi script

2

I was wondering if a batch script existed for automatically disabling an ethernet adaptor and enabling a wifi adaptor, I imagine that it'll require some fiddling with id's etc.

The reason for this is that my ethernet internet is metered, while my wifi is not. Hence I would like some easy way of switching between the two depending on whether I need the low packet loss of the ethernet connection.

The other possibility is whether there is a way to set certain programs to ignore an adaptor, i.e. make google chrome etc. not use ethernet, while everything else defaults to etherent?

End Result thanks to Seth

if( (Get-NetAdapter -Name "WiFi").Status -eq "Disabled"){
  Disable-NetAdapter -Name "Ethernet"
  Enable-NetAdapter -Name "Wifi"
  "Wifi enabled, Ethernet disabled"
}else{
  Disable-NetAdapter -Name "Wifi"
  Enable-NetAdapter -Name "Ethernet"
  "Ethernet enabled, Wifi disabled"
}

The reason that it checks for disabled rather than enabled, is due to there not being a status enabled (that I could see in my speed read) (that status is split into connecting etc.).

For my own convenience I made it into a batch file using the -EncodedCommand flag for powershell.

Cjen1

Posted 2016-11-07T13:42:46.097

Reputation: 125

Answers

5

If you look at this question: How can I write a batch file to toggle my network adapters?

You'll see that there are several options available. As you're saying you're running Windows 10. I'd rather use a PowerShell script. It could be as simple as combining Get-NetAdapter, Enable-NetAdapter and Disable-NetAdapter.

I don't have Windows 10 but it would propabably look something like this:

if( (Get-NetAdapter -Name "WLAN").Enabled -eq $True){
  Disable-NetAdapter -Name "WLAN"
  Enable-NetAdapter -Name "LAN"
}else{
  Disable-NetAdapter -Name "LAN"
  Enable-NetAdapter -Name "WLAN"
}

This doesn't have any error handling so there are probably a lot of cases which are not covered. If you'd wand to do this in a batch you could follow the same pattern but use netsh and it's output instead. Also have a look at the question for that is has some more information in that regard.

Seth

Posted 2016-11-07T13:42:46.097

Reputation: 7 657

Thanks thats just about perfect, just after I posted this I kind of realised that powershell maybe has a something and what you're answered is basically what I was starting to write so thanks for saving me a tonne of time. – Cjen1 – 2016-11-07T14:02:51.233

You will have to verify whenever Get-NetAdapter does expose an "Enabled" member or similar, as I said I don't have Windows 10 available right now. ;) – Seth – 2016-11-07T14:06:35.857

Yeah I'm having a look into it – Cjen1 – 2016-11-07T14:10:15.857

So it is exposed, but the issue is that disable/enable aren't doing anything, still looking into it, thanks for your help so far – Cjen1 – 2016-11-07T14:12:31.487

Better do gcm -noun netadapter to get on the right track – LotPings – 2016-11-07T14:30:03.997

1So I got the script finished and working, I'm going to post the result in the bottom of my question – Cjen1 – 2016-11-07T14:42:42.620