How can I run a script when my network connection changes?

I'd like to create a script to do various things when the network connection changes. For example, if the machine connects to my office network I might desire it to:

  • mute itself
  • change its DNS
  • change its proxy settings
  • change default printer

Then, when the machine is off the office network it might revert some of those.

Presumably the "active network" would be based on IP address/gateway range or WIFI network name, etc.

Do you have any recommended tools for accomplishing this? I can see something like this being applicable to many IT pros so I'm sure there are some good tips out there.

Note that this is desired to function when the network changes--not just when the user logs on/off.


The "Network Location Awareness" service (http://msdn.microsoft.com/en-us/library/ms739931(VS.85).aspx) in Windows XP (and 2000? I don't recall...) and up will enable this functionality, but I haven't found where anybody has written an application to take advantage of it. I'd love to code something myself, but I don't have enough spare cycles to even begin to think about it.

This wouldn't be a simple little VBScript thing, but it wouldn't be that much coding either. Maybe somebody could pick up the idea and run with it. There's even sample code at http://www.microsoft.com/downloads/details.aspx?familyid=ef8a6228-f11d-4ba0-b73e-dd8dc7dd11e8&displaylang=en.

There have been numerous times I've wanted this functionality, and I'd think there are more than a few people who would like to see it.


You can do it using Task Scheduler:

  1. Right-click on the "Task Scheduler Library"
  2. Click "Create Task"
  3. For "Name:" field write Your new task's name.
  4. Check that the "Security options" area has checkboxes where You want them to be.
  5. Under "Triggers" tab click "New..."
  6. Select "Begin the task:" => "On an event"
  7. Select "Log:" => "Microsoft-Windows-NetworkProfile/Operational"
  8. Select "Source:" => "NetworkProfile"
  9. Write "Event ID:" => 10000
  10. OK this dialog.
  11. Under "Actions" tab click "New..."
  12. Select "Action:" => "Start a program"
  13. For "Program/script:" browse to Your favorite script file.
  14. OK this dialog.
  15. Setup the "Conditions" tab regarding "Idle", "AC power" etc conditions to Your liking.
  16. Also do setup under "Settings" tab.
  17. OK, all done here.
  18. Test the new event.

Just in case, there are also events for computer "Suspend" and "Resume" available there. I, for example, use them to stop and restart Hamachi on these events, since otherwise it seems to have problems on my machine.
For computer "Suspend" You can use Log "Microsoft-Windows-Kernel-Power/Thermal-Operational", Source "Kernel-Power" and the Event ID is 42.
For computer "Resume" You can use Log "System", Source "Power-Troubleshooter" and the Event ID is 1.

For user login/logoff, I do not know events under Task Scheduler (but I believe they are available there too), but You can configure it instead here:

  1. Launch gpedit.msc
  2. Open "User Configuration"
  3. Open "Windows Settings"
  4. Click "Scripts (Logon/Logoff)"
  5. In the right pane You can assign scripts for "Logon" and "Logoff" events. Note, if You assign multiple scripts then do not assume that they are run synchronously. They may not. For this case use single script instead, that calls other scripts in sequence. I haven't checked under which account these scripts run - is it user or system?

You can do similar stuff for computer "Startup" and "Shutdown" events under "Computer Configuration" folder in the same window. The Startup script runs under System account before the user logs on.


I've had some success with Net Profiles before.