Click here to Skip to main content
13,044,467 members (60,177 online)
Click here to Skip to main content
Add your own
alternative version


4 bookmarked
Posted 31 May 2014

VirtualBox Physical Disk Import Batch Script

, 31 May 2014
Rate this:
Please Sign up or sign in to vote.
A Windows batch script to import a physical disk into a virtual machine


In Windows, it is not so obvious how to import a physical disk into a virtual machine. Therefore, I did some research, quickly found what I needed but as I had to use it more and more often, I wrote a batch script to do the work. This script will have the following functionality:

  • Check for administrative privileges
  • Find the install path of VirtualBox from registry
  • List all physical disk drives installed
  • Ask for the disk number to import
  • Check and eventually ask if the target file shall be overwritten


I am checking on many old hard disk drives of my company at the moment. I connect them using a USB-to-IDE cable and because there is a live disk encryption installed, I boot the disks with VirtualBox. Now the problem is that every time you change the disk, you have to create a new import-link file for that disk so that the correct parameters are set. So to make it nice and easy, I created this script.

The Manual Way

Usually, you would do the following to import a physical disk into Virtual Box. First, you need to find out which number your physical disk was mapped to. You can accomplish that by running 'diskmgmt.msc' and finding your disk there, It will look something like this:

Then you start a command prompt with administrator privileges, navigate to your VirtualBox install directory ('that one where VBoxManage.exe is located) and run the following command:

VBoxManage internalcommands createrawvmdk -filename C:\sd.vmdk -rawdisk \\.\PhysicalDrive#

where # is the disk number you have found. This will (if successful) create a small file 'C:\sd.vmdk' which you can then use in VirtualBox.

Using the Script

Basically the script is a wizard - just start it and follow the instructions.

When the script starts, it checks if it has administrative privileges. If it doesn't, the command-line text turns red, a VBS script is created that starts the script again with administrative privileges (prompting if configured in Windows) and than exits the first instance.

After that (assuming administrative privileges have been granted), it reads the Registry finding the install path of VirtualBox. If it could not be found, an error is shown.

Then it lists all physical drives installed and prompts for you to input the number of the disk to create a link file from - or to press [C] to cancel.

The scripts always chooses the file 'PhysicalDisk#.vmdk' in the same directory as the script as the target file. If the target file exists, you are asked if you want to overwrite it.

If everything went good (the scripts if the file was created), the text turns green - if not it turns red - and a fitting message is printed.

Using the Link File


When you started VirtualBox, go into your virtual machine's settings and switch to the 'Storage' section.

Click at 'Add Hard Disk' at the controller, click 'Choose existing disk', navigate to the created file and select it.

If this message pops up, you most probably did not start VirtualBox with administrator privileges.

To remove the virtual disk from VirtualBox, start the 'Virtual Media Manager' in the 'File' menu of VirtualBox, select the disk and click on 'Release' - then you can remove it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer
Germany Germany
I'm working mainly on .NET Compact Framework C# on mobile devices at work. At home it's .NET Full Framework C# and a bit JavaScript.

You may also be interested in...


Comments and Discussions

-- There are no messages in this forum --
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170713.1 | Last Updated 31 May 2014
Article Copyright 2014 by Bjørn
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid