There are plenty of articles around the Internet discussing how to enable or disable a Wi-Fi adapter based on the status of the wired connection. Whilst I’ve seen that some Dell laptops have this as a simple option in BIOS, our Lenovo machines do not, so this post describes how I achieved a workable solution.
Most modern NIC drivers log an event to the System log when a link is established or disconnected:
This means that it’s possible to create a scheduled task with triggers to look for those specific events, and run a script to enable or disable the wireless adapter.
Some people set up multiple tasks, and multiple scripts, to enable/disable the adapter, but I found it easier to create a single script that runs whenever any of these events occur:
- e1dexpress and e1cexpress – Event ID 32 (Link established)
- e1dexpress and e1cexpress – Event ID 27 (Link disconnected)
- On workstation unlock (useful if the computer is put to sleep, and then un-docked)
- At system startup
Note that the script may not work out-of-the-box if you don’t have a NIC that’s driven by one of the above drivers.
I currently push the script and the scheduled task configuration out using Group Policy Preferences.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Version 4.0 -RunAsAdministrator | |
<# | |
.SYNOPSIS | |
Manage-WiFiStatus.ps1 – Enable/Disable WiFi based on wired connectivity status | |
.DESCRIPTION | |
Disables the Wi-Fi adapter if the wired one is connected, and vice versa. | |
This is built for internal use on Windows 8.1, and depends on the new *-NetAdapter cmdlets in PS 4.0 | |
The point of this script is to run it based on System Event Log events that the NIC drivers | |
create when the physical NIC is plugged in or unplugged. Alternatively, you could just schedule it | |
to run periodically. | |
.OUTPUTS | |
Nothing | |
.LINK | |
.NOTES | |
Written By: Daniel Streefkerk | |
Website: http://daniel.streefkerkonline.com | |
Twitter: http://twitter.com/dstreefkerk | |
Change Log | |
v1.0, 06/03/2014 – Initial version | |
v1.1, 07/07/2015 – Replaced Write-Host with Write-Verbose | |
#> | |
# Get the NICs. This depends on them being named according to Windows 8's defaults | |
$wiredNic = Get-NetAdapter "ethernet" –ErrorAction SilentlyContinue –WarningAction SilentlyContinue | |
$wirelessNic = Get-NetAdapter "wi-fi" –ErrorAction SilentlyContinue –WarningAction SilentlyContinue | |
# If we can't figure out the NICs, bail out | |
If (($wiredNic -eq $null) -or ($wirelessNic -eq $null)) { | |
Write-Verbose "Couldn't find network adapters. Perhaps they're not named 'Ethernet' and 'Wi-Fi'?" | |
Exit | |
} | |
Write-Verbose "Wired NIC: $($wiredNic.InterfaceDescription)" | |
Write-Verbose "Wireless NIC: $($wirelessNic.InterfaceDescription)" | |
switch ($wiredNic.MediaConnectionState) { | |
"Connected" { | |
Write-Verbose "Physical NIC is connected. Disabling Wi-Fi Adapter" | |
$wirelessNic | Disable-NetAdapter –Confirm:$false | |
} | |
"Disconnected" { | |
Write-Verbose "Physical NIC is disconnected. Enabling Wi-Fi Adapter" | |
$wirelessNic | Enable-NetAdapter –Confirm:$false | |
} | |
} |
Note that this script depends on the network adapters having their default names as per Windows 8.x’s naming. Also, PowerShell 4.0 is required, as the script makes use of the newer *-NetAdapter cmdlets.
Hi ,
How do i identify event id for cellular in windows 10 if it goes down or up ? and is there any ways to switch to WIFI whenever my Cellular goes down ?
LikeLike
Hi,
It really depends on the hardware and drivers in use. Perhaps the cellular adapter doesn’t log any information. Or, you may need to enable a specific event log that’s disabled by default.
Sorry if that’s not much help…
LikeLike