Define a Task Manager trigger to start job when USB drive is connected


Buddy

Well-known member
Member
Local time
9:46 AM
Posts
237
Location
Memphis TN
OS
Windows 11 24H2
Like the title says...

I'd like to create a task in Task Manager that triggers when I plug in a specific USB drive. Is that possible? Does anyone know how to do it?
 
Windows Build/Version
22H2

My Computer My Computer

At a glance

Windows 11 24H2i5-1240p16gbWhatever comes in it
OS
Windows 11 24H2
Computer type
Laptop
Manufacturer/Model
Lenovo ThinkPad X1 Carbon G10
CPU
i5-1240p
Memory
16gb
Graphics Card(s)
Whatever comes in it
Sound Card
Whatever comes in it
Monitor(s) Displays
No external monitor. Yet.
Screen Resolution
1920 x 1200
Hard Drives
Internal 512 GB SSD
Desktop 6 TB, 1 TB, 225 GB, all HDDs
Portable 4TB SSD, 2TB HDD
A whole army of USB flash memory sticks
Mouse
Logitech M317
Internet Speed
500 mbps Fiber
Browser
Chrome
Antivirus
Windows Defender
Other Info
CalDigit TS4 dock for all my USB stuff, speakers, and connect to Android phone
HP MFP M277dw laser printer/scanner
I don't use it but maybe check Task Scheduler?
 

My Computers My Computers

  • At a glance

    Win11 Pro RTM Version 24H2 Build 26100.4202Intel Core i5 11th Gen. 2.40GHz12GB
    OS
    Win11 Pro RTM Version 24H2 Build 26100.4202
    Computer type
    Laptop
    Manufacturer/Model
    Dell Vostro 3400
    CPU
    Intel Core i5 11th Gen. 2.40GHz
    Memory
    12GB
    Hard Drives
    256GB SSD NVMe M.2
  • At a glance

    Win11 Pro RTM Version 24H2 Build 26100.4202Intel Core i5 10th Gen. 2.90GHz16GBOnboard, no VGA, using a DisplayPort-to-VGA a...
    Operating System
    Win11 Pro RTM Version 24H2 Build 26100.4202
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Vostro 5890
    CPU
    Intel Core i5 10th Gen. 2.90GHz
    Memory
    16GB
    Graphics card(s)
    Onboard, no VGA, using a DisplayPort-to-VGA adapter
    Monitor(s) Displays
    24" Dell
    Hard Drives
    512GB SSD NVMe, 4TB Seagate HDD
    Browser
    Firefox, Edge
    Antivirus
    Windows Defender/Microsoft Security
I don't use it but maybe check Task Scheduler?
Oops, my mistake. Yes, I meant Task Scheduler.

And I've already been poking around in it trying to find out how to use connection of a USB device as a trigger to start a task. No luck figuring that out yet.
 

My Computer My Computer

At a glance

Windows 11 24H2i5-1240p16gbWhatever comes in it
OS
Windows 11 24H2
Computer type
Laptop
Manufacturer/Model
Lenovo ThinkPad X1 Carbon G10
CPU
i5-1240p
Memory
16gb
Graphics Card(s)
Whatever comes in it
Sound Card
Whatever comes in it
Monitor(s) Displays
No external monitor. Yet.
Screen Resolution
1920 x 1200
Hard Drives
Internal 512 GB SSD
Desktop 6 TB, 1 TB, 225 GB, all HDDs
Portable 4TB SSD, 2TB HDD
A whole army of USB flash memory sticks
Mouse
Logitech M317
Internet Speed
500 mbps Fiber
Browser
Chrome
Antivirus
Windows Defender
Other Info
CalDigit TS4 dock for all my USB stuff, speakers, and connect to Android phone
HP MFP M277dw laser printer/scanner
How would the task compare with the autorun feature on CD and DVD discs?
 

My Computers My Computers

  • At a glance

    Win11 Pro RTM Version 24H2 Build 26100.4202Intel Core i5 11th Gen. 2.40GHz12GB
    OS
    Win11 Pro RTM Version 24H2 Build 26100.4202
    Computer type
    Laptop
    Manufacturer/Model
    Dell Vostro 3400
    CPU
    Intel Core i5 11th Gen. 2.40GHz
    Memory
    12GB
    Hard Drives
    256GB SSD NVMe M.2
  • At a glance

    Win11 Pro RTM Version 24H2 Build 26100.4202Intel Core i5 10th Gen. 2.90GHz16GBOnboard, no VGA, using a DisplayPort-to-VGA a...
    Operating System
    Win11 Pro RTM Version 24H2 Build 26100.4202
    Computer type
    PC/Desktop
    Manufacturer/Model
    Dell Vostro 5890
    CPU
    Intel Core i5 10th Gen. 2.90GHz
    Memory
    16GB
    Graphics card(s)
    Onboard, no VGA, using a DisplayPort-to-VGA adapter
    Monitor(s) Displays
    24" Dell
    Hard Drives
    512GB SSD NVMe, 4TB Seagate HDD
    Browser
    Firefox, Edge
    Antivirus
    Windows Defender/Microsoft Security
I do not think that there is an entry in the Event logs at the time a USB is connected so I think your only hope would be to have a continually-running script that tests for that particular USB drive and triggers your main job when it is detected.

If you are interested in this idea then I have a script that tests for specific drives by name [i.e. label] and that could be readily modified to repeat itself every 10 seconds or whatever you decide on.
Batch file attached - GetDriveLetter-SubRoutine.bat
You'll probably have lots of questions. The start of the batch file provides answers to all the questions I could think of.

I use the batch file as a sub-routine within other scripts as, I believe, you will also want to.
It is called by Call :IdentifyDrive "%Drivelabel%" in which the variable %Drivelabel% is the label of the target drive.
It returns the variable %DriveLetter% for use by the main body of the script.
- Note the line beginning :: Pause If you delete the :: then you'll be able to watch the script progressing.

In your case, your script's main body would test for %DriveLetter% not being empty {which equals a variable not being defined} i.e. a drive has been identified which has the label sought for.
In your case, you would want to repeat that test for %DriveLetter% not being empty every 10 seconds [or whatever interval you want] using the TimeOut command e.g. TimeOut /T 10
so your main body would start with something like this [example drive labelled Fred, test repeats every 10 seconds]

Set Drivelabel=Fred Set /a TestInterval=10 :RepeatedTesting TimeOut /T %TestInterval% Call :IdentifyDrive "%Drivelabel%" :: If Defined DriveLetter ((Echo Yes, I found it) & Pause) Else ((Echo Stop messing about) & Pause) If Defined DriveLetter (GoTo MainJobForThisUSB) Else (GoTo RepeatedTesting) :IdentifyDrive :: This is where you would copy in the code of GetDriveLetter-SubRoutine.bat :: You'll need to remove the above place label :IdentifyDrive when you copy the batch file in. I just put it in here so this code block made sense on its own. :MainJobForThisUSB :: This is where you write in whatever the job is that you are trying to do with that USB drive

- The line beginning :: If Defined gives you a chance to monitor the work of the drive label identification by removing the ::

I suggest you test GetDriveLetter-SubRoutine.bat on its own first and then add it to your main script when you are confident in its behaviour.
I have tested GetDriveLetter-SubRoutine.bat extensively. I've been using it for a few years now.
I wrote out the rest of the code in this post just for you and have not tested it. But it's so simple that I don't believe that there are any flaws in it.


Denis
 

Attachments

Last edited:

My Computer My Computer

At a glance

Windows 11 Home x64 Version 25H2 Build 26200....
OS
Windows 11 Home x64 Version 25H2 Build 26200.9168
How would the task compare with the autorun feature on CD and DVD discs?
Ooh! I'd forgotten about that. That might be the trick. I'll dig into it.
 

My Computer My Computer

At a glance

Windows 11 24H2i5-1240p16gbWhatever comes in it
OS
Windows 11 24H2
Computer type
Laptop
Manufacturer/Model
Lenovo ThinkPad X1 Carbon G10
CPU
i5-1240p
Memory
16gb
Graphics Card(s)
Whatever comes in it
Sound Card
Whatever comes in it
Monitor(s) Displays
No external monitor. Yet.
Screen Resolution
1920 x 1200
Hard Drives
Internal 512 GB SSD
Desktop 6 TB, 1 TB, 225 GB, all HDDs
Portable 4TB SSD, 2TB HDD
A whole army of USB flash memory sticks
Mouse
Logitech M317
Internet Speed
500 mbps Fiber
Browser
Chrome
Antivirus
Windows Defender
Other Info
CalDigit TS4 dock for all my USB stuff, speakers, and connect to Android phone
HP MFP M277dw laser printer/scanner

Latest Support Threads

Back
Top Bottom