New VirtualDisk - PowerShell


Overview

New-VirtualDisk creates a new virtual hard disk (VHD or VHDX) or a mounted image located in a remote location. It is commonly used for creating virtual machines, storing backups, or for various data storage scenarios.

Syntax

New-VirtualDisk [-Path] <String> -Size <Int64> [-Type] {Standard, Dynamic, Differencing} [-PassThrough] -AccessMode {ReadWrite, ReadOnly} -ImageFormat {VHD, VHDX, VMDK, QCOW2, RAW} [-AttachedDiskId] <String> [-MountPoint] <String> [-Force]

Options/Flags

  • Path: Specifies the path to the virtual disk file. If not specified, the current directory is used.
  • Size: Defines the size of the virtual disk in bytes.
  • Type: Specifies the type of virtual disk to create:
    • Standard: Creates a full-size virtual disk that can be read and written directly.
    • Dynamic: Creates a sparse virtual disk that expands dynamically as data is written.
    • Differencing: Creates a virtual disk that references another existing VHD and only stores the differences from it.
  • PassThrough: Enables pass-through disk mode, which directly exposes the underlying physical disk to the virtual machine.
  • AccessMode: Sets the access mode for the virtual disk:
    • ReadWrite: Allows read and write access to the disk.
    • ReadOnly: Restricts access to read-only mode.
  • ImageFormat: Selects the format used for the virtual disk file:
    • VHD: The default VHD format.
    • VHDX: An enhanced version of the VHD format.
    • VMDK: A format used by VMware.
    • QCOW2: A format used by KVM.
    • RAW: A disk image format that stores raw data without a file system.
  • AttachedDiskId: Assigns a unique identifier to the virtual disk to be used when attaching it to a virtual machine.
  • MountPoint: Mounts the virtual disk to a specific drive letter or path.
  • Force: Overwrites an existing virtual disk file if it already exists.

Examples

Creating a new standard virtual disk:

New-VirtualDisk -Path "C:\MyVirtualDisk.vhd" -Size 10240000000 -Type Standard

Creating a dynamic virtual disk with VHDX format:

New-VirtualDisk -Path "D:\MyVirtualDisk.vhdx" -Size 20480000000 -Type Dynamic -ImageFormat VHDX

Creating a pass-through virtual disk for a physical drive:

New-VirtualDisk -Path "PassThroughDisk.vhd" -PassThrough -AccessMode ReadWrite

Common Issues

  • Disk space: Ensure there is sufficient disk space on the target location to accommodate the virtual disk’s size.
  • Access permissions: Verify that the user has write permissions to the target location.
  • Virtualization enabled: On Hyper-V hosts, ensure virtualization is enabled in the BIOS settings.

Integration

Using New-VirtualDisk with New-VM: Creates a virtual machine with the specified virtual disk as its primary storage.

Combining with Mount-VHD: Mounts the virtual disk to a drive letter, making it accessible to the operating system.

  • Get-VirtualDisk
  • Remove-VirtualDisk
  • Mount-VHD
  • Convert-VHD