Trigger a Program Upon Connecting to a Specific Network in Windows

Depending upon the network you’re connected to, you may want to run certain tasks. For instance, when you’re connected to a specific network — e.g., home network — you may want to assign a different printer as the default, using command-line or script. This is especially needed in Windows 10 where the “location-aware printing” feature has been removed.

Or, you may need to run a backup task, or set the default save location in your Office apps to local documents instead of OneDrive when connected to a home network. Some users would want to turn on their VPN software upon connecting to a particular network.

Windows allows you to accomplish the task using event trigger and Task Scheduler. When you connect to a network, Windows logs the action to the Microsoft-Windows-NetworkProfile/Operational event log. The event ID is 10000 represents “connected” event and 10001 represents “disconnected” event. Here is a sample event.

trigger launch program when connecting to a specific network connection

Log Name: Microsoft-Windows-NetworkProfile/Operational
Source: Microsoft-Windows-NetworkProfile
Date: 7/30/2019 2:34:33 PM
Event ID: 10000
Task Category: None
Level: Information
Keywords: (35184372088832),(32)
User: LOCAL SERVICE
Computer: DESKTOP-JKJ4G5Q
Description:
Network Connected
Name: Ramesh
Desc: Ramesh
Type: Unmanaged
State: Connected
Category: Private

Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
<System>
<Provider Name="Microsoft-Windows-NetworkProfile" Guid="{fbcfac3f-8459-419f-8e48-1f0b49cdb85e}" />
<EventID>10000</EventID>
<Version>0</Version>
<Level>4</Level>
<Task>0</Task>
<Opcode>0</Opcode>
<Keywords>0x4000200000000020</Keywords>
<TimeCreated SystemTime="2019-07-30T09:04:33.524213200Z" />
<EventRecordID>3216</EventRecordID>
<Correlation />
<Execution ProcessID="2388" ThreadID="4596" />
<Channel>Microsoft-Windows-NetworkProfile/Operational</Channel>
<Computer>DESKTOP-JKJ4G5Q</Computer>
<Security UserID="S-1-5-19" />
</System>
<EventData>
<Data Name="Name">Ramesh</Data>
<Data Name="Description">Ramesh</Data>
<Data Name="Guid">{f6435db3-31b3-43ea-8d7f-4c28208a954d}</Data>
<Data Name="Type">0</Data>
<Data Name="State">1</Data>
<Data Name="Category">1</Data>
</EventData>
</Event>

The bold line above represents the network connection, in this case, the network name or the SSID is Ramesh.

Now, let’s create an event trigger for event ID 10000 Microsoft-Windows-NetworkProfile/Operational event. The event trigger scheduled task will launch a program or script.



You can use the Attach Task To This Event… option in the event’s right-click menu in Event Viewer to create an event trigger task. Alternately, you can follow these Task Scheduler steps.

Launch a Program or Script when Connected to a Specific Network

  1. Open Task Scheduler and click Create Task…
  2. Assign a name and description for the task.
  3. Select the Triggers tab, and click New
  4. Select On an event
  5. In the Log: dropdown box, select Microsoft-Windows-NetworkProfile/Operational
  6. In the Source: select NetworkProfile
  7. In the Event ID: field, type 10000, and click OK
  8. In the Actions tab, click New.
  9. In the Program/Script: name, type the program or script file name you want to run when your device connects to a specific network. For example, you may choose to run a custom VBScript that executes some commands upon connecting to that network. To run a VBScript, choose wscript.exe in the Program/Script: field, and the name of the script (.vbs file) in the Add arguments: field.
    trigger launch program when connecting to a specific network connection
  10. Click OK.
  11. Select the Conditions tab, and enable Start only if the following network connection is available.
  12. Select the network or SSID from the list so that the scheduled task will trigger when you connect to the specific network.
    trigger launch program when connecting to a specific network connection
  13. If the program or script requires administrator privileges to run correctly, make sure to enable the Run with highest privileges checkbox, and click OK.
    trigger launch program when connecting to a specific network connection
  14. Click OK. The event trigger task is now created.

Task not triggered on network condition in Windows 10?

In Windows 10 Anniversary Update and higher, the task may not get triggered if a network condition is set in the GUI. This bug still remains in Windows 10 v1903. In some systems, setting a network condition causes the error Task Scheduler service is not available. Task Scheduler will attempt to reconnect to it.

To workaround the problem, disable all network conditions and use the following custom trigger method.

  1. Double-click the newly created task.
  2. Select the Conditions tab, and uncheck Start only if the following network connection is available.
  3. Select the Triggers tab, and click Edit.
  4. In the Edit Trigger dialog, click Custom, and click New Event Filter…
    trigger launch program when connecting to a specific network connection
  5. In the New Event Filter, select the XML tab, and enable Edit query manually.
  6. Click Yes, when you see the message: If you choose to manually edit the query, you will no longer be able to modify the query using the controls on the Filter tab. Would you like to continue?
  7. Paste the following custom XML trigger markup in the text box.
    <QueryList>
    <Query Id="0" Path="System">
    <Select Path="Microsoft-Windows-NetworkProfile/Operational">
    *[System[(EventID=10000)]] and *[EventData[(Data[@Name="Name"]="TYPE-YOUR-SSID")]]
    </Select>
    </Query>
    </QueryList>

    If need to trigger the task when you connect to an active directory domain network, use the domain name instead of the SSID.

  8. Click OK, OK.

Similarly, you may create another event trigger scheduled task that runs when you connect to your office network, which in turn launches a custom VBScript, a program or a batch file.


One small request: If you liked this post, please share this?

One "tiny" share from you would seriously help a lot with the growth of this blog. Some great suggestions:
  • Pin it!
  • Share it to your favorite blog + Facebook, Reddit
  • Tweet it!
So thank you so much for your support, my reader. It won't take more than 10 seconds of your time. The share buttons are right below. :)

About the author

Ramesh Srinivasan founded Winhelponline.com back in 2005. He is passionate about Microsoft technologies and he has been a Microsoft Most Valuable Professional (MVP) for 10 consecutive years from 2003 to 2012.

1 thought on “Trigger a Program Upon Connecting to a Specific Network in Windows”

Leave a Comment