Search code examples
powershellvhd

Making a VHD image of a drive, using PowerShell


I would like to make a VHD image of a harddisk.

I know how to make VHD images (http://technet.microsoft.com/en-us/library/hh848503.aspx) but i would like the image to contain alle the content from an existing drive.

The solution im currently thinking about is making a VHD the same size as the source drive, and then copy all files over from the source drive, but i think there got to be a cleaner (easier) way.

I know about Drive2Vhd from Sysinternals, but i need to do this with PowerShell.

Any suggestions?


Solution

  • If you really want to avoid using Disk2vhd you can wrap two excellent exes inside of some powershell logic to get the job done.

    To be more manual about how you create and load it with data you run the following commands that you wrapped inside of a powershell script.

    Step 1 - Create the VHD in Diskpart

    > diskpart
    DISKPART> create vdisk file=”c:\test\test.vhd” maximum=1000
    DISKPART> select vdisk file="c:\test\test.vhd"
    DISKPART> attach vdisk
    DISKPART> create partition primary
    DISKPART> assign letter=X noerr
    DISKPART> exit
    

    Step 2 - Copy the files into your new vhd

    Robocopy <Source> <Destination> *.* /S /MT:128
    

    Step 3 - Detach the vhd

    select vdisk file="c:\test\test.vhd"
    detach vdisk
    exit