Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I found out that Windows 7 doesn't allow autorun on USB drives.

Is it possible to circumvent this (in a safe way) by setting up a task with Task Scheduler, which fires when the USB drive is inserted? At a first glance I couldn't find any related events with Event Viewer.

share|improve this question

5 Answers 5

up vote 4 down vote accepted

I had the same question as you, and worked out something with powershell (windows built-in scripting) using techniques from the Scripting Guy Blog here and here. The script runs continuously as a background process, which you can start at system logon with task scheduler. The script will be notified whenever a new drive is plugged and then do something (here you configure the script rather than the task). Since it is basically paused while waiting for the next plugged drive, you should not find it takes up much resources. Here I go:

1) Start Powershell ISE, which can be found in your start menu under Accessories/Windows Powershell.

2) Copy paste the following into Powershell.

#Requires -version 2.0
Register-WmiEvent -Class win32_VolumeChangeEvent -SourceIdentifier volumeChange
write-host (get-date -format s) "     Beginning script..."
do{
  $newEvent = Wait-Event -SourceIdentifier volumeChange
  $eventType = $newEvent.SourceEventArgs.NewEvent.EventType
  $eventTypeName = switch($eventType)
    {
    1 {"Configuration changed"}
    2 {"Device arrival"}
    3 {"Device removal"}
    4 {"docking"}
    }
  write-host (get-date -format s) "     Event detected = " $eventTypeName
  if ($eventType -eq 2)
  {
    $driveLetter = $newEvent.SourceEventArgs.NewEvent.DriveName
    $driveLabel = ([wmi]"Win32_LogicalDisk='$driveLetter'").VolumeName
    write-host (get-date -format s) "     Drive name = " $driveLetter
    write-host (get-date -format s) "     Drive label = " $driveLabel
    # Execute process if drive matches specified condition(s)
    if ($driveLetter -eq 'Z:' -and $driveLabel -eq 'Mirror')
    {
        write-host (get-date -format s) "     Starting task in 3 seconds..."
     start-sleep -seconds 3
        start-process "Z:\sync.bat"
    }
  }
  Remove-Event -SourceIdentifier volumeChange
} while (1-eq1) #Loop until next event
Unregister-Event -SourceIdentifier volumeChange

3) You need to modify the script above to tell the script what drive to look for, and what to execute. The two lines to change are:

  • if ($driveLetter -eq 'Z:' -and $driveLabel -eq 'Mirror')

    My usb hard drive named 'Mirror' is set as the Z: drive. You could just use if ($driveLabel -eq 'MyDiskLabel') if you didn't care about the letter.

  • start-process "Z:\sync.bat"

    Path of whatever task you want to do. In my example, I have created a batch file on my USB drive which start 3-4 backup tasks, but it could be anything.

4) When you're done, save your script somewhere (extension .ps1), then go create a task in Task Scheduler to have your script run in background. Mine looks like this:

  • Trigger: At log on
  • Action: Start a program
  • Program/script: powershell
  • Add arguments: -ExecutionPolicy Unrestricted -File "D:\Stuff\Backup script.ps1"

5) Voilà!

6) Extra stuff: If you want your script window to be hidden, use these arguments:

  • Add arguments: -WindowStyle Hidden -ExecutionPolicy Unrestricted -File "D:\Stuff\Backup script.ps1"

If you want to output the script messages into a log file (that gets overwritten everytime the script starts, i.e. at log on), use the following task action:

  • Program/script: cmd
  • Add arguments: /c powershell -WindowStyle Hidden -ExecutionPolicy Unrestricted -File "D:\Stuff\Backup script.ps1" > "D:\Stuff\script log.txt"

Anytime you want to end the running hidden script, you can end the "Powershell" process in Task Manager.

share|improve this answer
1  
Very nice! No external programs needed. I wonder why signing the scripts has been made so difficult --- seems to encourage setting the execution policy to unrestricted. –  Ville Koskinen Jan 10 '10 at 15:45

No. You can't use Task Scheduler.

The only logged event that fires when you plug your USB device in, is to start the 'Portable Device Enumeration' service. But you can't use it in Task Manager because the same event fires when any service is started and stopped. (Plus the event won't fire if the service is already running when you plug your device in.)

share|improve this answer

USB Safely Remove has such a feature: it can start a program when a USB device is inserted.

Another USB Safely Remove feature that distinguishes it from similar software is starting any applications not only after connecting a device, but also before removing it. The autorun feature allows you to set up data backup before disconnecting a removable hard drive, to run Total Commander with the contents of the pen-drive, automatically unmount an encrypted TrueCrypt drive before disconnecting the USB media, etc.

alt text

share|improve this answer

I don't think a task scheduler would be the proper tool. You need something that would respond every time a new USB device is added. That would require a specialized application that can detect new USB devices and thus it would replace the Windows autorun feature.

Yes, such an application is possible. It would be a challenge to create, though.

Using the task scheduler means you're just going to start autorun over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over and over (well, you get the picture) again. You would need a mechanism to prevent using the autorun twice. And you need a mechanism that detects if there's an autorun available or not, else you end up with everlasting error messages. It's like hitting the wall with a hammer, hoping there's a nail in-between. The scheduler wouldn't know when to start and when to stop.

share|improve this answer

See if this article helps you : http://mintywhite.com/tech/windows-7/windows-seven-7-set-autoplay-defaults-how-to/.

share|improve this answer
    
AutoRun and AutoPlay are different things –  subman Aug 29 '09 at 12:40
1  
Autoplay on Windows 7 has a "Software and games" option that can be set to "Install or run programs from your media". I haven't tested its effect, but it looks suspiciously like Autorun. –  harrymc Aug 29 '09 at 13:10
    
Yes, but because Autorun has been disabled for USB devices it doesn't work for them. –  subman Aug 29 '09 at 13:30
    
I've seen claims that U3 has solved the usb launch problems in Windows 7, together with promises by Microsoft that U3 will function on it. So there might be a solution, but I don't have the time to research it. –  harrymc Aug 29 '09 at 16:54
    
It's possible with U3 because it is emulating CDROM. –  Maciek Sawicki Jan 10 '10 at 4:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.