Script to attach a USB device to a virtual machine [VPC]

Script to attach a USB device to a virtual machine [VPC]

  • Comments 14

A couple of people have asked me how to automate attaching a USB device to a Windows Virtual PC virtual machine, so here is a PowerShell script to do just that:

# Connect to Virtual PC
$vpc = new-object -com VirtualPC.Application
# Get VM name
$vmName = Read-host "Specify the name of the virtual machine that you want to use"
# List available USB devices
write-host "The following USB devices are available:"
$vpc.USBDeviceCollection | select -ExpandProperty DeviceString
# Get the USB device name
$usb = Read-host "Enter the name of the USB device that you want to connect to the virtual machine"
# Get the VM object
$vm = $vpc.findVirtualMachine($vmName)
# Get the USB object
$usbDevice = $vpc.USBDeviceCollection | ? {$_.DeviceString -eq $usb} | select -first 1
# Attach the device - this will fail if the VM is not running

Note that I am using the “DeviceString” of the USB device – which is not guaranteed to be unique.  So what I do is just grab the first one that matches.


Leave a Comment
  • Please add 5 and 4 and type the answer here:
  • Post
  • Can you write a script that would work for a hyper-v vm?

  • You can't connect USB devices to Hyper-V

  • @Dave, not as such.  But if it's a USB drive, you can attach the whole drive as a SCSI device.

  • @Richard, but you're connecting the USB drive to the host, not a VM. So why would you need a script for that?

  • I tried the script and discovered (to no great surprise) that it requires integration components to be installed. I'm trying to test out several Win7 install/deployment methods and would love a way to configure a second (virtual) DVD drive, a virtual floppy drive, or some other type of storage attached to the virtual machine that would allow the Windown setup process to access the xml file. Any idea on how to get that done? Have I overlooked something obvious? I've got 22 browser tabs open, all dealing with ways to get this deployment done, and this script was one of the my last attempts.

  • For other looking, I found using vbscript much easier than getting powerscript running. Here is an excelent example I followed:

    Also, if unfamiliar with vbscript, the free trial program VbsEdit is great for debugging and understanding the objects and properties used in the example.

  • I am not a programmer.

    What do i do with the script ?

    (I have visual studio if necessary)

  • Hey guys, what do I have to do with this script in order use it? Do i just run it with Windows Power Shell? Do I have to imput the name of the device in the script?


  • I have tried using your script on powershell (64-bit) unforunately it does not work.

    here are the messages which i received,

    You cannot call a method on a null-valued expression.

    At C:\scripts\AttachUSBDevice.ps2:21 char:20

    + $vm.AttachUSBDevice <<<< ($usbDevice)

    + CategoryInfo: InvalidOperation: <AttachUSBDevice:String> [], RuntimeExcepton

    + FullyQalifiedErrorId: invokeMethodOnNull

  • Didn't work for me.   $vmName should be "Windows XP Mode" and I checked that the $vm variable was a valid object by at the command line.  

    My USB Device does not have a DeivceString, since it it doesn't work under Vista (or else I wouldn't bother with this whole exercise anyway!), so I identify it by the Port number.   I checked that $usbDevice and $vm were both valid, and when trying to execute the line $vm.AttachUSBDevice($usbDevice)

     and I get the following error :

    Exception calling "AttachUSBDevice" with "1" argument(s): "Could not attach the USB device. You can attach the    USB devi

    ce to a different USB port or restart the virtual machine and try again."

    At line:1 char:20

    + $vm.AttachUSBDevice <<<< ($usbDevice)

       + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

       + FullyQualifiedErrorId : ComMethodTargetInvocation

    This is with Virtual PC up and running.   If its not running I am unable to find the VM so dont' have a valid $vm variable (which makes sense)

  • I know it's 2013, but I just found myself searching for just this kind of thing; It's still coming in handy. Thank you!

  • Thanks Ben

    Just used this script to attach a USB port on Windows XP Mode VirtualPC on a Windows 7 host. USB port is used for software licensing. Script work perfectly

    Cheers, Paul

  • Yes...Thank you Ben.  I used this script to attach to an USB connected DenOptix Imaging Scanner on Windows XP Mode VirtualPC on a Windows 7 host.  There are no Windows 7 drivers so had to do the Virtual PC thing and the script worked perfectly.  Actually running your script on a task attached to an Event...great stuff.  Thanks again.

  • CJD2000 - Can you elaborate on how you got this to work with a DenOptix? I'm dealing with the identical situation right now. Thank you

Page 1 of 1 (14 items)