Eka
Eka

Reputation: 15000

Batch sript for running a file when a usb is plugged

Is there any batch script which can be used to run a file kept in your system(eg: C:\Program Files\abc.exe) when a pendrive is pluged in.

What i am thinking is this batch script when executed will add or modify some registry keys thus enabling the file in the system (this case: C:\Program Files\abc.exe) to run when a pendrive is inserted

Upvotes: 0

Views: 1116

Answers (2)

alroc
alroc

Reputation: 28144

This isn't a batch solution, but WMI has functionality built in that you can use for this. See this answer to a similar question.

In your case, the code would be:

$query = "SELECT * FROM Win32_DeviceChangeEvent WHERE EventType = 2"
Register-WMIEvent -Query $query -Action { invoke-item "C:\Program Files\abc.exe"}

If you need to look for (or filter out) specific pendrives, more work would be involved.

Upvotes: 1

Programmer Paul
Programmer Paul

Reputation: 508

I would add to SmokeyPHP's answer that you could put an identifying file on the pendrive. Something with a unique name. Your script that runs on a schedule could enumerate the available drives and look for that file name. It doesn't seem super efficient, but I don't know of a trigger for USB things being added.

Upvotes: 0

Related Questions