Skip to content

Instantly share code, notes, and snippets.

@dstreefkerk
Last active March 19, 2021 02:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dstreefkerk/9380335 to your computer and use it in GitHub Desktop.
Save dstreefkerk/9380335 to your computer and use it in GitHub Desktop.
#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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment