SCVMM – Custom inventory about VM and VHD with PowerShell

Whenever I go to a new customer with a Hyper-V and SCVMM environment, there are a bunch of scripts I run in order to get a view of their environment.
One of those is to get all VM’s, the the VHD sizes and when there is a Dynamic VHD the appropiate information (size, maximum size, free space, percentage in use) is shown in red.
Why red? Because I don’t think that using Dynamic VHD’s in a production environment, with the current release of Hyper-V, is not the best road to take.
The script provides an output like:

I hope you find the following usefulGlimlach Here’s the code:

$VMS = Get-VM -VMMServer localhost | Sort-Object -Property Name -Descending
“<HTML><HEAD><TITLE>VM Inventory</TITLE></HEAD><BODY><TABLE BORDER=1>”  | Out-File $Env:Temp\VMInventory.html
“<TR><TD>VM Name</TD><TD>VM Memory</TD><TD>VHD Name</TD><TD>VHD Length</TD><TD>VHD Maximum</TD><TD>VHD Available</TD><TD>VHD Free %</TD></TR>” | Out-File $Env:Temp\VMInventory.html -Append
foreach ($VM in $VMS)
“<TR><TD>”+$VM.Name+”</TD><TD>”+$VM.Memory+”MB</TD><TD>.</TD><TD>.</TD><TD>.</TD><TD>.</TD><TD>.</TD></TR>” | Out-File $Env:Temp\VMInventory.html -Append
foreach ( $VHD in $VM.VirtualHarddisks)
$VHDLength = ($VHD.Size / 1GB)
$VHDSize = “{0:N2}” -f $VHDLength
if ($VHD.VHDType -eq “DynamicallyExpanding”)
$BackgroundColor = “#FF0000”
$VHDMaximumSize = “{0:N2}” -f ($VHD.MaximumSize / 1GB)
$VHDAvailable = “{0:N2}” -f (($VHD.MaximumSize – $VHD.Size) /1GB)
$VHDFree = “{0:N2}” -f (100 – (($VHD.Size * 100) / $VHD.MaximumSize))
“<TR><TD>.</TD><TD>.</TD><TD bgcolor=”+$BackgroundColor+”>”+$VHD.Name+”</TD><TD bgcolor=”+$BackgroundColor+”>”+$VHDSize+”GB</TD><TD bgcolor=”+$BackgroundColor+”>”+$VHDMaximumSize+”</TD><TD bgcolor=”+$BackgroundColor+”>”+$VHDAvailable+”</TD><TD bgcolor=”+$BackgroundColor+”>”+$VHDFree+” %</TD></TR>” | Out-File $Env:Temp\VMInventory.html -Append
“<TR><TD>.</TD><TD>.</TD><TD>”+$VHD.Name+”</TD><TD>”+$VHDSize+”GB</TD><TD>.</TD><TD>.</TD><TD>.</TD></TR>” | Out-File $Env:Temp\VMInventory.html -Append
“<TR>” | Out-File $Env:Temp\vminventory.html -Append
“</TABLE><BODY></HTML>” | Out-File $Env:Temp\VMInventory.html -Append
Invoke-Expression $Env:Temp\vminventory.html


  1. Dwooldridge says:

    This is a great script!  However, when I run the script, the VHD Free % displays the incorrect information-
    VHD Length:  18.77GB
    VHD Max:  127.00
    VHD Available:  108.23
    VHD Free %:  14.78%

    The correct VHD Free % should actually be 85% since 108.23/127=.852
    Other than that, it’s a great little script, thanks for sharing!

  2. Jeff Wouters says:

    Hi Dwooldridge,Good one! Nasty little math error, changed the code :-)Thanks for noticing and posting your comment.Jeff.

  3. Mark says:

    Hi, your script has a small typo I think with missing quotes after: $BackgroundColor = “#FF0000 < here
    Thanks for the script!

  4. Jeff Wouters says:

    Hi Mark,
    It seems that the browser is messing with my font somehow… I saw the same thing you did but when I copy-paste the line it shows the double quotes…
    Thanks for noticing and commenting it, though 🙂

  5. Yerunski says:

    The code doesn’t work??
    I get the error:
    PS C:UsersAdministrator> .overzicht.ps1
    Unexpected token ‘{‘ in expression or statement.
    At C:UsersAdministratoroverzicht.ps1:14 char:21
    + $VHDMaximumSize = “{ <<<< 0:N2}" -f ($VHD.MaximumSize / 1GB)
    + CategoryInfo : ParserError: ({:String) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToken

  6. Jeff Wouters says:

    I’ve just tested the code on an enviroment from a different customer and it works just fine over here…
    Have you changed the code perhaps? What version of VMM are you using? What version of PowerShell are you using? (it’s tested on PowerShell v2)

  7. Jake Sully says:

    Hi, This is fantastic, would you mind posting your other scripts to share as well? I just started a new job where there’s nothing documented/inventoried and I am trying to build some scripts, but I’m putting out fires all days, so if you post them I would appreciate it!

  8. Jeff Wouters says:

    Hi Jake,
    I post mostly short scripts which give you same basic building blocks for creating a larger script such as the one in this post. Whenever I think a script has added value to the community, I’ll post it on my blog. Posting larger scripts such as the one in this post is not something I do a lot… only when I think the script has added value and I haven’t found something like it anywhere else on the internet.
    In your case it may be easier to learn PowerShell instead so that you can write your own scripts and offer great value to your customer and/or colleagues.
    Good luck in putting out fires though… and I hope you’ll find some of my other posts useful aswell 🙂

  9. chaba_ok says:

    Hi, this is awesome script. It help me a lot. I found one point for further improvement. If the disk is .avhd (snapshot), it will show snapshot size not a size of logical disk on that vm.

  10. Jeff Wouters says:

    Hi Chaba_OK,
    Good feedback!
    I’ll put it on my to-script list 🙂

  11. chaba_ok says:

    Hi Jeff,
    Please accept my apology, my problem is differencing disk not snapshot. The result of differencing disk does not include parent disk which is reasonable. Your script is working correctly.


  12. Jeff Wouters says:

    Hi Chaba_OK,
    Accepted, no poblem 🙂 Thanks for the re-check and feedback 🙂

  13. jotheman says:

    In 2013 this is still a great script 🙂

    We had this issue as well. This is due to copy/pasting wrongly.

    To fix this: Manually change these marks “ by ” marks (normal quotation marks) and change the quotation marks around these calculations “{0:N2}” by ‘{0:N2}’
    Then it will work.
    Jeff, nice scripting!

Leave a Reply

Your email address will not be published. Required fields are marked *