rss logo

How to Automatically Execute a Task on USB Drive Insertion in Windows

USB Certified logo in the form of a cinema clapper, representing USB certification, with a modern design combining a clapper icon and the text USB.

In this article, I'm going to explain how to run a task as soon as a USB flash drive is connected to a Windows computer. To do this, I'll use the Windows task scheduler, which is triggered when event number 112 appears in the DeviceSetupManager log.

Personally, I use it mainly to run antivirus scan on users computers.

So let's take a detailed look at how to create a Windows task and the parameters to be entered so that it can run a program when a USB flash drive is inserted.

Create a Windows Task

  • Type taskchd.msc inside a Run window:
Windows Run dialog box with 'taskschd.msc' entered to open the Task Scheduler.
  • Create a task:
Windows Task Scheduler interface highlighting the 'Create Task' option in the context menu.

Set parameters

  • Give the task a name (ex: USB_scan), select the user under which the task will run (ex: SYSTEM) and hide the task:
Windows Task Scheduler 'Create Task' window showing configuration settings such as task name, user account, and options to run with highest privileges and hide the task.
  • Create a new Action:
Windows Task Scheduler 'Create Task' window highlighting the 'New' button in the Actions tab to specify an action for the task.
  • Exemple here with Kaspersky Endpoint Security:
    • Program/script: "C:\Program Files (x86)\Kaspersky Lab\Kaspersky Endpoint Security for Windows\avp.exe"
    • Add arguments: SCAN /REMDRIVES
Windows Task Scheduler 'New Action' window showing the configuration of a task to start a program with specific arguments, such as a Kaspersky antivirus scan for removable drives.
  • Now go to the Triggers tab and click New…:
Windows Task Scheduler 'Create Task' window highlighting the 'Triggers' tab and the 'New' button to set up a new trigger for the task.
  • Set parameters:
    • Log: Microsoft-Windows-DeviceSetupManager/Admin
    • Source: DeviceSetupManager
    • Event ID: 112
Windows Task Scheduler 'New Trigger' window showing the configuration for triggering a task on an event, with details such as log, source, and event ID set to 112.

Now, plug in a USB flash drive and check that a scan is running.

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Contact :

contact mail address