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

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

  1. Saddam ZEMMALI (@zemmali1990)

    $report = ForEach ($VM in Get-VM ){
    ($VM.Extensiondata.Guest.Disk | Select
    @{N=”Data Center”;E={$vm | Get-Datacenter | Select-Object -ExpandProperty name }},
    @{N=”vCenter Server”;E={$vm.ExtensionData.Client.ServiceUrl.Split(‘/’)[2].trimend(“:443″)}},
    @{N=”Cluster”;E={$vm | Get-Cluster | Select-Object -ExpandProperty name}},
    @{N=”Host”;E={$VM.VMHost}},
    @{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)}})
    }
    $report | Export-Csv D:\SZEMMALI\scripts\vmware-collect\report.csv -NoTypeInformation -UseCulture

  2. Pingback: Getting VMware guest disk information – HyperPowered Thoughts

  3. suraj

    Great Script , in need to export the output for this script in .csv , please can you help

  4. Pingback: Menampilkan Informasi Pada VMware Menggunakan PowerShell – nyubi.net

  5. Raju G

    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

  6. liavk

    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

  7. Rob

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

  8. Lenny Chauhan

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

  9. Tom Howarth

    when I run this one liner it format on the screen as a list rather than the table that is displayed above. adding format-table does not help either

  10. HeM

    Great script .. but how can I find all VMS (HARD DISK )info withing a particular Cluster

  11. KM

    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

  12. KM

    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

  13. sanjai

    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?

  14. KM

    Hello friends,

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

    Thanks in advance.

  15. Pingback: Virtu-Al | Virtually everything is poshable

  16. Morry

    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

  17. Grant

    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

  18. Virtu-Al

    @Lars, @Abdul,

    Yes I have the code which exports this to CSV, I will post it over the next couple of days.

  19. Lars

    Hi Al,
    Greate script, it helps me a lot!
    Is there a way to export the output to a .XLS file?

  20. Pingback: VMTN Podcast number 70, Storage… » Yellow Bricks

  21. Virtu-Al

    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 🙂

  22. CP

    This is a very nice script, thanks.

    Is there a way to have this exported to a file?

  23. Roger

    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

  24. Virtu-Al

    @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

  25. Albert Widjaja

    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

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

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