Skip to content

Get-HyperVVMVHDDetail

Hyper-V: Audits virtual hard disk storage details and controller paths

#Requires -Version 5.1
#Requires -Modules Hyper-V

[CmdletBinding()]
Param (
    [string]$ComputerName = "localhost",

    [PSCredential]$Credential,

    [Parameter(Mandatory = $true)]
    [string]$Name
)

Process {
    try {
        $params = @{
            'ComputerName' = $ComputerName
            'ErrorAction'  = 'Stop'
        }
        if ($Credential) { $params.Add('Credential', $Credential) }

        $vm = Get-VM @params | Where-Object { $_.Name -eq $Name -or $_.Id -eq $Name }

        if (-not $vm) {
            throw "Virtual machine '$Name' not found on '$ComputerName'."
        }

        $vhdDrives = Get-VMHardDiskDrive -VM $vm -ErrorAction Stop
        
        $results = foreach ($drive in $vhdDrives) {
            $vhd = Get-VHD -ComputerName $ComputerName -Path $drive.Path -ErrorAction Stop
            
            [PSCustomObject]@{
                VMName           = $vm.Name
                ControllerType   = $drive.ControllerType
                ControllerNumber = $drive.ControllerNumber
                ControllerLocation = $drive.ControllerLocation
                Path             = $drive.Path
                VhdType          = $vhd.VhdType
                VhdFormat        = $vhd.VhdFormat
                FileSizeGB       = [math]::Round($vhd.FileSize / 1GB, 2)
                SizeGB           = [math]::Round($vhd.Size / 1GB, 2)
                Timestamp        = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
            }
        }

        Write-Output $results
    }
    catch {
        throw
    }
}

Specifies the name of the Hyper-V host. Defaults to the local machine.

Specifies the credentials to use for the remote connection.

Specifies the name or ID of the virtual machine.

An interactive directory of PowerShell scripts.