VI Toolkit One-Liner: VM Guest Disk Sizes

A Simple One-liner today but it shows you how to add information along the pipe line to enable new properties which can be formed using other cmdlets or simple math statements on existing properties:

This one-liner will give you the VM disk or partition sizes for each of your VMs:

ForEach ($VM in Get-VM ){($VM.Extensiondata.Guest.Disk | Select @{N="Name";E={$VM.Name}},DiskPath, @{N="Capacity(MB)";E={[math]::Round($_.Capacity/ 1MB)}}, @{N="Free Space(MB)";E={[math]::Round($_.FreeSpace / 1MB)}}, @{N="Free Space %";E={[math]::Round(((100* ($_.FreeSpace))/ ($_.Capacity)),0)}})}

Output:

image

30 thoughts on “VI Toolkit One-Liner: VM Guest Disk Sizes”

  1. Hi All,
    I tried the script and trying to export it in html, but the data is not coming in html.. can any one help me in that.

    ForEach ($VM in ( Get-VM |Get-View)){($info = $VM.Guest.Disk |Select @{N=“Name“;E={$VM.Name}},DiskPath, @{N=“Capacity(GB)“;E={[math]::Round($_.Capacity/ 1GB)}}, @{N=“Free Space(GB)“;E={[math]::Round($_.FreeSpace / 1GB)}}, @{N=“Free Space %“;E={[math]::Round(((100 * ($_.FreeSpace))/ ($_.Capacity)),0)}})|Format-Table} ConvertTo-Html | Set-Content C:\Users\ABC\Desktop\FreeSpaceFinal.htm

  2. hi
     i need some help
     im using this script for specific VMs in vApp
     ForEach
    ($VM in (Get-VApp “My vApp” | Get-VM |Get-View)){($VM.Guest.Disk
    |Select @{N=“Name“;E={$VM.Name}},DiskPath,
    @{N=“Capacity(GB)“;E={[math]::Round($_.Capacity/ 1GB)}}, @{N=“Free
    Space(GB)“;E={[math]::Round($_.FreeSpace / 1GB)}}, @{N=“Free Space
    %“;E={[math]::Round(((100 * ($_.FreeSpace))/
    ($_.Capacity)),0)}})|Format-Table}
     
    i want to add “if” option that send me email when the “free Space %” is lower than 15
    please help

  3. I read about VI Toolkit, but cannot find a download URL anywhere.
    Can you give me an idea from where to obtain it?

  4. I am looking for a script that will list vmdk type (Thin, Lazy Zero Thick, Eager Zero Thick) for all the guests.

  5. you can get with this below script

    Connect-VIServer

    $AllVMs = Get-VM | Get-View | Where {-not $_.Config.Template}
    $SortedVMs = $AllVMs | Select *, @{N=”NumDisks”;E={@($_.Guest.Disk.Length)}} | Sort-Object -Descending NumDisks

    }
    }}
    } | Out-GridView

    Disconnect-VIServer -Confirm:$False

  6. Connect-VIServer

    $AllVMs = Get-VM | Get-View | Where {-not $_.Config.Template}
    $SortedVMs = $AllVMs | Select *, @{N=”NumDisks”;E={@($_.Guest.Disk.Length)}} | Sort-Object -Descending NumDisks

    }
    }}
    } | Out-GridView

    Disconnect-VIServer -Confirm:$False

  7. Alan,

    This is a one liner that works perfect.

    Get-VM | Where-Object {$_.Name -match “vmware”} | select Name , powerstate , Numcpu , memorymb

    but i wanted to add the “VM IP” and “VM creation date” to the same. is it possible?

  8. Hello friends,

    It’s really helping to my work, i want IP & HostName also can any one help me on this ?

    Thanks in advance.

  9. Hi there,

    Im trying to get this in an Script… which gets the Size and writes all VM’s with Free Diskspace less then an value in an HTML or textfile to E-Mail it…

    Anybody.. could help… started today with PowerCLI / Powershell
    For the E-Mail List I would like to use the Variable $users
    and for the WarningValue $percentofWarning…

    The E-Mail sending-sequenze using the Content of the txt/html is already done..

    Any ideas?

    regs
    Morry

  10. Fantastic – Thanks very much – I’ve been trying (and not getting very far) to put something like this together.

    Question – Is it overly complicated to add additional code to update the annotation field for each VM with this information?

    Regards
    Grant

  11. Yes there is a way but i doubt it will be a one-liner, I will try and work on it when I get 5 mins 🙂

  12. Hello Al,

    You script(oneliner) works great for windows guests but only shows the Boot volume for a Linux machine, do you know a way how to list the VMDK sizes for a Linux VM ?

    Roger

  13. @Albert Widjaja

    Apparently this is a bit of an issue with PowerGui, if you are running this through the PowerGui editor then try running it by pushing Ctrl + F5 rather than just F5 or pushing the play button. (Thanks LucD)

    Hope that helps.

    Alan

  14. Dear Sir,

    Thanks for creating and sharing such a great cmdlet, however when
    I execute your script in PowerGUI

    after specifying: Connect-VIServer ESXi1

    i got error:
    Operation is not valid due to the current state of the object.
    At :line:0 char:0

    is it because I’m running ESXi 3.5 u4 ?

    thanks.

Leave a Reply