Virtu-Al.Net

Virtually everything is poshable

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 |Get-View)){($VM.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)}}) | Format-Table}

 

Output:

image

, ,

19 Responses to “VI Toolkit One-Liner: VM Guest Disk Sizes”

  • Chris says:

    Great Simple Script. Thanks

  • Albert Widjaja says:

    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.

  • Virtu-Al says:

    @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

  • Roger says:

    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

  • Virtu-Al says:

    @Roger
    Hmmm, well spotted, will work on that one, no promises it will be a single line though :)

  • CP says:

    This is a very nice script, thanks.

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

  • Virtu-Al says:

    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 :)

  • [...] Virtu-Al – VI Toolkit One-Liner: VM Guest Disk Sizes [...]

  • Lars says:

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

  • Abdul says:

    Did you find a way to export it to a file?

  • Virtu-Al says:

    @Lars, @Abdul,

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

  • Grant says:

    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

  • Morry says:

    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

  • [...] I have had a few comments on an old post I wrote showing a one-liner to get the VM’s disk sizes for each VM, I was asked if there was a [...]

  • KM says:

    Hello friends,

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

    Thanks in advance.

  • sanjai says:

    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?

  • KM says:

    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

  • KM says:

    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

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>