Network switching with Powershell
Faced with a super-annoying network configuration where some tasks require being connected to a LAN with proxy configured, but most internet based tasks fail through the proxy. I have never met a proxy that made my life easier… PowerShell to the rescue!
Disable wifi when the LAN is connected:
Save the following file as Disable-wifi.ps1:
# This will list the names of all your adapters:
# Get-NetAdapter | Format-Table Name, InterfaceDescription -Auto
$regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
if ((Get-NetAdapter -Name 'Local Area Connection').Status -ne 'Disconnected')
{
# Enable LAN
Disable-NetAdapter -Name 'Wireless Network Connection' -confirm:$False -AsJob | Wait-Job
# Enable proxy
Set-ItemProperty -path $regKey ProxyEnable -value 1
# Log it
Add-Content -Path "C:\Andy\Tools\network.log" -Value "$(Get-Date),enabled LAN"
}
Create a new scheduled task as follows:
The key is to set the task to be triggered by a Windows event, in this case essentially “WLAN becoming operational”.
And set the task to run our PowerShell script.
To enable wifi when the LAN is disconnected
Save the following file as Enable-wifi.ps1:
$regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
if ((Get-NetAdapter -Name 'Local Area Connection').Status -eq 'Disconnected')
{
# Enable Wifi
Enable-NetAdapter -Name 'Wireless Network Connection' -confirm:$False
Set-ItemProperty -path $regKey ProxyEnable -value 0
# Log it
Add-Content -Path "network.log" -Value "$(Get-Date),enabled wifi"
}
Now create another scheduled task running the enable script.
In this case the event code to trigger the script is different:
And we have an additional condition before running to ensure the network adapter is available. Maybe this isn’t always needed but seemed to make it more reliable:
Less annoying mucking around with network connections = win!