How to change hosts entries on network changes

[Reading Time: 5 minutes]

[sgmb id=”1″]Why?

I am changing some special hosts file entries according to different networks, I am connecting to.
One is with Direct Access (DA) into my companies’ network, that works with IPv4 address resolution. The other one is directly connecting to the office’s network, using IPv6.
So I have to change my entries by hand, to reflect address resolution, every time, when I am changing between these two networks… and that is really annoying.

After a couple of years (right…! doing this day by day with the goal, finding a way out next day (every day), I passed over years ) and many, difficult to track, issues, that could easy by solved by not forgetting to change theses entries in hosts file, I tried to find a solution….

…and here it is!

It is that simple, that I shouldn’t post it here, to save my face 🙂. But I think, I am not alone with that.

Windows Tasks Scheduler is the key. So, let me explain:

Think of a simple hosts file entry:



(Currently I am connected by DA over IPv4, therefore I uncomment the second line and commented out the first one.)

For automatically changing these settings to the invert, I can (and of course, there are other possible solution) create a Tasks, that runs on detected NetworkProfile change. With a little powershell script the right settings in the hosts file will be modified. So have an eye on the following instuction:

  1. Open Windows Tasks-Scheduler
  2. Create new Task by right clicking somewhere in Task Schedulers tree
  3. In next dialog enter a Name for the task
    1. (it’s up to you, to decide, whether to choose running with logged on user or not)
    2. Making changes to hosts file is only with administrative privileges possible, so click “Run with highest privileges
    3. Also set the configuration in respect to the running machine
  4. Switch to tab “Triggers
    1. Select at “Begin the task:On an event
    2. Search for Microsoft-Windows-NetworkProfile/Operational at “Log:
    3. On “Source:” select NetworkProfile
    4. As “Event-ID:” enter 1000 (means “Network changed”)
  5. OK… coming slowly to an End…
    1. Switch one tab further to “Actions
    2. Leave “Start a program” in “Action:
    3. Now we want a Powershell script to be triggered on the networkchanged-event
    4. So… add as “Program/Script” name PowerShell.exe
    5. As “Add arguments (optional):” add script’s path
  6. If you are not willing to spend the time into selecting the right event, but want to achieve the same result here is a simpler solution
    1. Switch to tab “Conditions
    2. Select last checkbox and choose the network that triggers
  7. Save everything an go to next step….

The triggered powershell script

Now, everything is ready to run something an the networkchange event. So here come the script (I do not have to mention, that there are more elegant ways):

$profile = (Get-NetConnectionProfile).Name
$hostsPath = "$env:windir\System32\drivers\etc\hosts"
Write-Host $profile
if($profile -contains 'MyWLan'){
# Uncomment lines for current profile
$hosts = get-content $hostsPath
$hosts = $hosts | Foreach {if ($_ -match '^\s*#\s*(.*?\d{1,3}.*?wlan.*)')
                           {$matches[1]} else {$_}}
$hosts | Out-File $hostsPath -enc ascii
# Comment lines for the other profile:
$hosts = get-content $hostsPath
$hosts | Foreach {if ($_ -match '^\s*([^#].*?\d{1,3}.*?home.*)') 
                  {"# " + $matches[1]} else {$_}} |
         Out-File $hostsPath -enc ascii
# Uncomment lines for current profile
$hosts = get-content $hostsPath
$hosts = $hosts | Foreach {if ($_ -match '^\s*#\s*(.*?\d{1,3}.*?home.*)')
                           {$matches[1]} else {$_}}
$hosts | Out-File $hostsPath -enc ascii
# Comment lines for the other profile:
$hosts = get-content $hostsPath
$hosts | Foreach {if ($_ -match '^\s*([^#].*?\d{1,3}.*?wlan.*)') 
                  {"# " + $matches[1]} else {$_}} |
         Out-File $hostsPath -enc ascii

(again my example hosts file)


In the first line the script retrieves the network profile name (the name, that is also listed in that combobox in picture 6.b.). In relation to the name of the current netw.profile, all lines in the hosts file, that contain the string “wlan” will be uncommented and others with “home” commented.

The result after network switch is :

By Thomas

As Chief Technology Officer at Xpirit Germany. I am responsible for driving productivity for our customers by a full stack of dev and technology in modern times. But I not only care for technologies from Microsofts stack like Azure, AI, and IoT, but also for delivering quality and expertise with DevOps

Leave a Reply

Your email address will not be published. Required fields are marked *

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