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.
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
- Open Task Scheduler and click Create Task…
- Assign a name and description for the task.
- Select the Triggers tab, and click New
- Select On an event
- In the Log: dropdown box, select Microsoft-Windows-NetworkProfile/Operational
- In the Source: select NetworkProfile
- In the Event ID: field, type
10000
, and click OK - In the Actions tab, click New.
- 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.
- Click OK.
- Select the Conditions tab, and enable Start only if the following network connection is available.
- Select the network or SSID from the list so that the scheduled task will trigger when you connect to the specific network.
- If the program or script requires administrator privileges to run correctly, make sure to enable the Run with highest privileges checkbox, and click OK.
- 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.
- Double-click the newly created task.
- Select the Conditions tab, and uncheck Start only if the following network connection is available.
- Select the Triggers tab, and click Edit.
- In the Edit Trigger dialog, click Custom, and click New Event Filter…
- In the New Event Filter, select the XML tab, and enable Edit query manually.
- 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?
- 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.
- 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!
Wow, it worked beautifully.
Thanks !
Shouldn’t and doesn’t work correctly.
If you’re connected to the specified network already, then this action will incorrectly fire upon connection of to other networks.
@Walter: Can you please elaborate why
This almost gets me to what I need. I have a first trigger setup for a specific time. So when I add this trigger, as an event, ‘Multiple Triggers’ shows up. And it appears that *either* trigger will trigger the event I need to occur. I need this to work as an ‘and’ type procedure, in that both triggers must be true to cause the event to occur (At the specific time, check to see if online with the correct network, and if so, only then run the event I need to occur.) How can I do this? I don’t know enough about the xml routine above to add this myself. Any help would be greatly appreciated!
I loved the article and idea but found that it wasn’t exactly what I wanted either, I wanted to initiate my vpn connection when signing on if I was connected to my home network, XML option was great if I connect to my home network after signing in but I want the conditional of during sign in connect to vpn if I’m on my home network. My solution was to keep the task scheduler trigger to be at logon of user and run a single line powershell command to perform the secondary conditional and have it execute the final command if I’m on my home network. May not be the cleanest but it gets the job done as desired:
Action:
powershell.exe
Argument:
“
if (invoke-command -scriptblock {get-netconnectionprofile | where { $_.Name -match 'mynetworknamehere' }}) {Invoke-Command -ScriptBlock { start-process -FilePath 'C:\Test\Sample.executableorscript' } }
“This works great! Thanks so much!
@Jeremy: Excellent idea. thanks for posting that.
This works great! Thanks so much! Although I’ve been aware of powershell, I’ve never used it before. A quick net search brought me to “-WindowStyle Hidden”, which I added to the argument section so I don’t have to be interrupted with the blue screen. Thanks again!