Get The List of All VMs in HyperV Cluster and Their Virtual Hard Disks With PowerShell

2 Min. Read

A while ago, I published a PowerShell script that will query System Center Virtual Machine Manager (SCVMM) to get all Virtual Machines including their Virtual Hard Disks, then it will calculate the size and percentage used for each VHD(X), and finally send you a nicely formatted HTML report. You can read all about it here.

In this quick blog post, I will share with you how to get the same report in comma-separated value (CSV) format and without SCVMM. This comes in handy when working with a large Hyper-V cluster with many VMs, and you want to know the type of each virtual hard disk attached to every VM including their footprint size on disk, and the remaining disk space so you know before the VM runs out of disk space.

Here is a function that will do the job for you.

Function Get-VMVHDs {

<#
    .Synopsis
        A script used to find all VHD files for all VMs in a Hyper-V Cluster

    .DESCRIPTION
        A script used to find all VHD files for all VMs in a Hyper-V Cluster,
        including the type of each virtual hard disk attached to every VM with their footprint size on disk,
        and the remaining disk space, so you know before the VM runs out of disk space.
        Finally, it will export the report into a csv file.
        
    .Notes
        Created   : 2018-12-03
        Version   : 1.0
        OS        : Windows Server 2016 Hyper-V or later
        Author    : Charbel Nemnom
        Twitter   : @CharbelNemnom
        Blog      : https://charbelnemnom.com
        Disclaimer: This script is provided "AS IS" with no warranties.

    .EXAMPLE
        . .\Get-VMVHDs.ps1
        Get-VMVHDs -Cluster <ClusterName>
    #>   

[CmdletBinding(SupportsShouldProcess=$true)]
    Param(
    [string]$ClusterName
    )

Get-ClusterGroup -Cluster $ClusterName | ? {$_.GroupType -eq 'VirtualMachine' } | Get-VM | `
% { $vhd = Get-VHD -ComputerName $_.ComputerName -VmId $_.VmId; $vhd | Add-Member -NotePropertyName “Name” -NotePropertyValue $_.Name; $vhd; } | `
Select-Object Name, @{label=’Host Name’;expression={$_.ComputerName}}, Path, VhdFormat, VhdType, @{label=’Size On Physical Disk (GB)’;expression={$_.FileSize/1gb –as [int]}}, `
@{label=’Max Disk Size (GB)’;expression={$_.Size/1gb –as [int]}}, @{label=’Remaining Space (GB)’;expression={($_.Size/1gb - $_.FileSize/1gb) –as [int]}} | `
Export-Csv -Path “C:\$($ClusterName)-VHDReport.csv" -NoTypeInformation

}

And here is the final report in CSV:

Get The List of All VMs in HyperV Cluster and Their Virtual Hard Disks With PowerShell 2

This is version 1.0, do you want additional features? Please feel free to leave a comment below.

Enjoy :)

__
Thank you for reading my blog.

If you have any questions or feedback, please leave a comment.

-Charbel Nemnom-

Previous

How To Be an MVP In Life Not Only In Technology #MVPBuzz #MVPInLife

How To Monitor Azure File Sync #AzureFiles #AzureMonitor #AFS

Next

2 thoughts on “Get The List of All VMs in HyperV Cluster and Their Virtual Hard Disks With PowerShell”

Leave a comment...

  1. Hi , Can you add OS to the export as well?

  2. Hello Michael,
    I usually set the OS Name and the VM Name identical.
    The script is returning the VM Name which is the computer name.
    Please give it a try and let me know if it works for you.
    Thanks!

Let me know what you think, or ask a question...

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Subscribe to Stay in Touch

Never miss out on your favorite posts and our latest announcements!

The content of this website is copyrighted from being plagiarized!

You can copy from the 'Code Blocks' in 'Black' by selecting the Code.

Please send your feedback to the author using this form for any 'Code' you like.

Thank you for visiting!