How to Automatically Execute a Task on USB Drive Insertion in Windows
- Last updated: Jan 25, 2025

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
taskschd.msc
inside aRun
window:

- Create a task:

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:

- Create a new Action:

- 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
- Program/script:

- Now go to the Triggers tab and click New…:

- Set parameters:
- Log:
Microsoft-Windows-DeviceSetupManager/Admin
- Source:
DeviceSetupManager
- Event ID:
112
- Log:

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