Freigeben über


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

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
 $vm.AttachUSBDevice($usbDevice)

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.

Cheers,
Ben

AttachUSBDevice.zip

Comments

  • Anonymous
    January 19, 2011
    Can you write a script that would work for a hyper-v vm?

  • Anonymous
    January 19, 2011
    You can't connect USB devices to Hyper-V

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

  • Anonymous
    January 25, 2011
    The comment has been removed

  • Anonymous
    February 05, 2011
    The comment has been removed

  • Anonymous
    February 07, 2011
    For other looking, I found using vbscript much easier than getting powerscript running. Here is an excelent example I followed: social.technet.microsoft.com/.../031e26d1-7cd6-4d35-ae5a-4d8457631386 Also, if unfamiliar with vbscript, the free trial program VbsEdit is great for debugging and understanding the objects and properties used in the example.

  • Anonymous
    March 21, 2011
    I am not a programmer. What do i do with the script ? (I have visual studio if necessary)

  • Anonymous
    December 22, 2011
    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? Thanks.

  • Anonymous
    March 08, 2012
    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:scriptsAttachUSBDevice.ps2:21 char:20

  • $vm.AttachUSBDevice <<<< ($usbDevice)
  • CategoryInfo: InvalidOperation: <AttachUSBDevice:String> [], RuntimeExcepton
  • FullyQalifiedErrorId: invokeMethodOnNull
  • Anonymous
    September 24, 2012
    The comment has been removed

  • Anonymous
    April 28, 2013
    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!

  • Anonymous
    April 16, 2014
    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

  • Anonymous
    May 01, 2014
    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.

  • Anonymous
    May 15, 2014
    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