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


Buddy

Active member
Member
Local time
8:06 AM
Posts
196
Location
Memphis TN
OS
Windows 11 22H2
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

System One

  • OS
    Windows 11 22H2
    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
    External 6 TB, 1 TB, 225 GB desktop HDD, 2TB portable HDD
    A whole army of USB flash memory sticks
    Mouse
    Logitech M317
    Internet Speed
    500 mbps
    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

System One System Two

  • OS
    Win11 Pro RTM
    Computer type
    Laptop
    Manufacturer/Model
    Dell Vostro 3400
    CPU
    Intel Core i5 11th Gen. 2.40GHz
    Memory
    12GB
    Hard Drives
    256GB SSD NVMe
  • Operating System
    Windows 11 Pro RTM x64
    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, 2TB WDC 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

System One

  • OS
    Windows 11 22H2
    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
    External 6 TB, 1 TB, 225 GB desktop HDD, 2TB portable HDD
    A whole army of USB flash memory sticks
    Mouse
    Logitech M317
    Internet Speed
    500 mbps
    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

System One System Two

  • OS
    Win11 Pro RTM
    Computer type
    Laptop
    Manufacturer/Model
    Dell Vostro 3400
    CPU
    Intel Core i5 11th Gen. 2.40GHz
    Memory
    12GB
    Hard Drives
    256GB SSD NVMe
  • Operating System
    Windows 11 Pro RTM x64
    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, 2TB WDC 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

  • GetDriveLetter-SubRoutine.bat
    2.9 KB · Views: 10
Last edited:

My Computer

System One

  • OS
    Windows 11 Home x64 Version 23H2 Build 22631.3296
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

System One

  • OS
    Windows 11 22H2
    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
    External 6 TB, 1 TB, 225 GB desktop HDD, 2TB portable HDD
    A whole army of USB flash memory sticks
    Mouse
    Logitech M317
    Internet Speed
    500 mbps
    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
Back
Top Bottom