Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using the following powershell script to detect when a particular volume is mounted so I can run a script that will move files from my machine to the device (I don't know much about powershell scripts, I found this online).

#Requires -version 2.0
Register-WmiEvent -Class win32_VolumeChangeEvent -SourceIdentifier volumeChange
write-host (get-date -format s) "     Beginning script..."
  $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 'G:' -and $driveLabel -eq 'My Book')
        write-host (get-date -format s) "     Starting task in 5 seconds..."
     start-sleep -seconds 5
        start-process "F:\copy_backups.bat"
  Remove-Event -SourceIdentifier volumeChange
} while (1-eq1) #Loop until next event
Unregister-Event -SourceIdentifier volumeChange

G is a physical external hdd and F is a truecrypt container within G. When the script detects the correct device is mounted as G, it sleeps 5 seconds to give truecrypt time to mount F, and then runs the script found on F. It appears that volume change events are only generated when the physical drive is connected/disconnected (at least that's the only time the script receives an event), because leaving G connected and mounting/dismounting F does not trigger the script. I would like to be able to detect when the truecrypt container is mounted without anything else changing. At some level this must be possible, because windows explorer updates its drives display when the container is mounted or dismounted. Thanks for the help.

share|improve this question
Are you trying to know when any volume is mounted, or specifically TrueCrypt volumes? – Robert Kerr May 5 '13 at 16:49
I think you'd have better luck with this on stackoverflow. – Robert Kerr May 5 '13 at 16:53
the desired behavior is to execute a script when a particular truecrypt volume is mounted – Matthew Patey May 5 '13 at 16:54

I don't quite fully understand the script you've provided, but I think this bit may be of use to you. It's not as neat as using WMI triggers, but it should work.

# The while loop in this script will naturally generate errors until F is mounted.
# This line shuts PowerShell up for awhile.
$ErrorActionPreference = 'SilentlyContinue'

# Loop the script until F is mounted.
while ($FMounted -eq $null)
    $FMounted = Get-PSDrive F

# Reset $ErrorActionPreference to default.
$ErrorActionPreference = 'Continue'

Tested in PS 3.0 on Windows 7 Ultimate x64. Should be fairly backwards-compatible though.

EDIT: Well, my tests of the above were a bit more rudimentary and did work. But the full script seems to break a bit, at least in the ISE. Here's what was working for me in the PowerShell console.

$ErrorActionPreference = 'SilentlyContinue'
$x = Get-PSDrive X
while ($x -eq $null) {$x = Get-PSDrive X};echo 'X is mounted'

The command would properly hang until I mounted a TrueCrypt drive to X, then it ran the echo command and terminated.

I fleshed the script out a bit more in the ISE and tried to make the full do ... while loop like you had, adding another while loop to hang the script until X was unmounted. It worked once to run an echo command when the drive was mounted and again when unmounted. However, since then, Get-PSDrive from within the ISE does not show X regardless of its actual status.

I started a new PowerShell console in the ISE and ran the full script again, this time starting with X already mounted. The first while loop successfully exited, but the while loop conditioned on $x -ne $null stayed stuck even after my drive was unmounted. Killing the script and checking Get-PSDrive showed that that PowerShell instance was still showing X as available even though it wasn't really there.

Maybe you'll have better luck or find a workaround. Please let me know if this does help, and if you figure out a fix.

share|improve this answer

Your Answer


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.