Hosts file depending on network connection

What if I want to apply a specific hosts file depending on my network connection? When I’m at work I’m using the ethernet connection. At home it’s the wi-fi. Obviously I’m not using the same resources at work and at home. Here’s a few lines to get the right hosts file applied


$wifi=Get-NetAdapter | Where-Object Name -Match "^Wireless.*" | Select -ExpandProperty Status
if ($wifi -eq "Disabled") { Copy-Item C:\Windows\System32\drivers\etc\lan-hosts C:\Windows\System32\drivers\etc\hosts }
else if ($wifi -eq "Up") { Copy-Item C:\Windows\System32\drivers\etc\wifi-hosts C:\Windows\System32\drivers\etc\hosts }
else { Write-Host "Unable to determine the network connection status" }

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top