Figure 2.20 The basic information about a virtual hard disk shown by the Inspect
Disk option
While graphical tools are highly intuitive for automation, a scripting language is
typically leveraged, and PowerShell is the command-line interface and scripting
language of choice for Microsoft.
Here are some of my favorite PowerShell commands related to virtual hard disks.
Note that you do not have to specify whether a disk is VHD or VHDX. Simply setting
the file type to VHD or VHDX lets the commands know the type of virtual hard disk to
create. I’ll show you later in the book how to create new virtual hard disks as part of a
virtual machine creation.
Here’s the command to create a fixed VHDX file:
New-VHD -Path D:\Virtuals\newfix.vhdx -Fixed -SizeBytes 10GB
To create a dynamic VHDX file, use this command:
New-VHD -Path D:\Virtuals\newdyn.vhdx -Dynamic -SizeBytes 1TB
The command to create a differencing VHDX file is as follows:
New-VHD -ParentPath D:\Virtuals\newfix.vhdx '
-Path D:\Virtuals\newdif.vhdx -Differencing '
-SizeBytes 1000000000
Here is the command to create a VHDX using very large block sizes:
New-VHD –Path D:\Virtuals\LargeBlockSize.vhdx –BlockSizeBytes 128MB '
–LogicalSectorSize 4KB –SizeBytes 1TB
A virtual hard disk can be added to a virtual machine by using the Add-
VMHardDiskDrive PowerShell cmdlet, as in this example: