Creating a virtual drive in Windows 7 is a fairly simple task but I wanted to create a PowerShell script that would make things just a bit easier to perform. This script contains a function (“Create-VhdDrive”) that only requires 2 parameters (path and filename) to create the new VHD disk but it does allow you to override all of the possibilities so you can create a fixed verses expandable, set the size, identify the drive letter to mount the new drive to, etc.
This script creates a VHD file, formats it and mounts it to your system. It does not attempt to set up the drive for “Boot to VHD”.
The script requires that you are an administrator and when it runs the “Are you sure you want to do this” dialog will appear.
As the script runs a dialog box may appear like the one below:
Cancel out of that window since the script does format the drive for you.
When the script is done, the auto-play dialog will appear.
In fact, it may appear before you have a chance to cancel out of the format dialog. This may happen if you create an expandable drive since an expandable drive does not require setting aside all of the required space ahead of time.
Once that dialog appears, the drive has been created and is ready for use.
Here is the script (there is an example of a call to the main function (“Create-VhdDrive”) at the bottom of the script):
# CreateVhd.ps1 # # Summary: This script will create and attach a Windows 7 Virtual Hard Drive (VHD) # The Virtual Hard Drive is essentially a file that can be attached to a Windows 7 # machine in such a way that it appears and acts just as any other drive on your # computer. Benefits include attaching and unattaching at will, being able to back # up the drive simply by copying a single file, ability to create fixed or expandable # drives, ability to create any size drive up to multiple terabytes (assuming your # physical hard drive has the space), etc. # function: Check-AvailableDrive # This function will check to see if a drive letter is available. This is important # when trying to assign and attach a drive. The drive letter must not already be # in use. function Check-AvailableDrive { param ( $driveLetterToSearch ) foreach( $driveFound in [System.IO.DriveInfo]::GetDrives()) { if ($driveLetterToSearch + ":`\" -eq $driveFound.Name) { return $false } # the drive letter is in use? } # Drive letter is available return $true } # function: Create-VhdDrive # This function generates a script and passes that script to the DiskPart utility # to generate a formatted VHD drive at a specified location. # $vdiskPath (Required) is the drive:\folder location of the new VHD drive file # $vdiskFileName (required) is the name of the new VHD file to create (Note: the file does not have to # end with ".vhd" for it to work but it is the standard. # The rest of the parameters are optional and have default values function Create-VhdDrive { param ( $vdiskPath = $(Throw "vdiskPath was not specified"), $vdiskFileName = $(Throw "vdiskFileName was not specified"), $vdiskMaximumSize = 20000, $vdiskType = "EXPANDABLE", $vdiskDriveLetter = "", $vdiskVolumeName = "NewVHD", $vdiskScriptFileName = "CreateVhd.scr" ) # Validate the drive letter if ($vdiskDriveLetter.length -eq 0) { $driveAssign = "" } else { if ($vdiskDriveLetter.length -ne 1) { Throw "Error: the variable `"driveLetter`" must be a single letter only." } else { if ((Check-AvailableDrive $vdiskDriveLetter) -eq $false) { $errorResult = "Cannot create drive $vdiskDriveLetter - it already exists." Throw $errorResult } $driveAssign = "LETTER=$vdiskDriveLetter" } } # Validate the disk type to be FIXED or EXPANDABLE if (($vdiskType -ne "FIXED") -and ($vdiskType -ne "EXPANDABLE")) { Throw "Error: vdiskType must be `"FIXED`" or `"EXPANDABLE`"" } # Validate that the destination path for the new file does indeed exists if (![System.IO.Directory]::Exists($vdiskPath)) { Throw "Error: the destination folder does not exist - $vdiskPath" } # Create the full path for the new file $vdiskFullFilePath = [System.IO.Path]::Combine($vdiskPath, $vdiskFileName) # Generate the text for the script $scriptLineToWrite = "CREATE VDISK FILE=`"$vdiskFullFilePath`" MAXIMUM=$vdiskMaximumSize TYPE=$vdiskType`n" $scriptLineToWrite = $scriptLineToWrite + "SELECT VDISK FILE=`"$vdiskFullFilePath`"`n" $scriptLineToWrite = $scriptLineToWrite + "ATTACH VDISK`n" $scriptLineToWrite = $scriptLineToWrite + "CREATE PARTITION PRIMARY`n" $scriptLineToWrite = $scriptLineToWrite + "ASSIGN $driveAssign`n" $scriptLineToWrite = $scriptLineToWrite + "FORMAT QUICK LABEL=$vdiskVolumeName" # Write the script out to disk so it can be passed into DiskPart $scriptPath = [System.IO.Path]::Combine($vdiskPath, $vdiskScriptFileName) $scriptLineToWrite | out-file -encoding ASCII $scriptPath # Create the VHD Disk File diskpart /s $scriptPath } # Example function call to create a 30GB expandable file: Create-VhdDrive -vdiskPath "d:\bootfromvhd" -vdiskFileName "MyNewVhdDisk2.vhd" -vdiskMaximumSize "10000" -vdiskVolumeName "MyVhdDrive01"