PowerCLI: Virtual Machine disk usage
Recently 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 way to export this information into a CSV file.
The easy answer is yes but what you need to do is build up a container and then add each part of the information to the container, this is quite common practice in PowerShell, its a great technique where you can basically build and populate your own information and then just add to it, once you are completed you can then take your container and export it to whatever format you wish, for example:
The one-liner mentioned in my previous post simply outputted the data one VM at a time to the screen, whilst this was great for looking at the information, when we try and export it things start to go wrong !
The new script, below, may take up more lines but is far more efficient and adaptable.
The script:
Connect-VIServer MYVISERVER
$MyCollection = @()
$AllVMs = Get-View -ViewType VirtualMachine | Where {-not $_.Config.Template}
$SortedVMs = $AllVMs | Select *, @{N="NumDisks";E={@($_.Guest.Disk.Length)}} | Sort-Object -Descending NumDisks
ForEach ($VM in $SortedVMs){
$Details = New-object PSObject
$Details | Add-Member -Name Name -Value $VM.name -Membertype NoteProperty
$DiskNum = 0
Foreach ($disk in $VM.Guest.Disk){
$Details | Add-Member -Name "Disk$($DiskNum)path" -MemberType NoteProperty -Value $Disk.DiskPath
$Details | Add-Member -Name "Disk$($DiskNum)Capacity(MB)" -MemberType NoteProperty -Value ([math]::Round($disk.Capacity/ 1MB))
$Details | Add-Member -Name "Disk$($DiskNum)FreeSpace(MB)" -MemberType NoteProperty -Value ([math]::Round($disk.FreeSpace / 1MB))
$DiskNum++
}
$MyCollection += $Details
}
$MyCollection | Out-GridView
# Export-Csv, ConvertTo-Html or ConvertTo-Xml can be used above instead of Out-Gridview
The output is listed in a strange order due to an issue with this method of using PowerShell as mentioned here.
Just a reminder that this information can also be viewed, filtered and exported using my VESI/PowerGUI PowerPack which you can download here. – Nearly 5000 downloads so far !
Top 10 Virtualisation blogger PowerCLI: Working with events











Thanks, just what i’m looking for.
Social comments and analytics for this post…
This post was mentioned on Twitter by alanrenouf: New BlogPost: PowerCLI: Virtual Machine disk usage – http://tinyurl.com/y89p4nr #PowerCLI #VESI #PowerGUI…
Your article was most tweeted by Virtualization experts in the Twitterverse…
Come see other top popular articles surfaced by Virtualization experts!…
Thanks. Works great.
Not sure if it’s my environment only, but can it show linux partitions (mount names)? Seems to show only Windows drive letters…
@Kayser
Thanks for the comment, Yes this also works on Linux VM’s but you will need to make sure you have VMTools installed and running for it to grab the data.
Just installed vmtools on the linux vms this script is perfect. You’re the man, thanks very much.
[...] look at the “advanced” script of virtu-al (http://www.virtu-al.net/2010/01/27/powercli-virtual-machine-disk-usage/) VN:F [1.7.7_1013]please wait…Rating: 0.0/5 (0 votes cast) Share [...]
This is awesome — but whats the trick to get the VMId? I’m sure its another
“$Details | Add-Member -Name VMId -Value $VM.SomeDataMember -Membertype NoteProperty”
But every data member I’ve tried thusfar comes back empty …
Which ID are you talking about, can you give me an example please ?
The “vmid” — the one like ‘VirtualMachine-vm-4087′
As written the script will grab every vm guest disk size for all vm’s seen by the VI server.
How can I limit this to specific datacenters?
I made a modification others may find useful: I added a member named UsedSpace(MB) and calculated the guest vm used disk space (Total – Free).
@Don Maslanka
On line 5 where it reads:
$AllVMs = Get-View -ViewType VirtualMachine | Where {-not $_.Config.Template}
Chage it to:
$AllVMs = Get-Datacenter “My Datacenter” | Get-VM | Get-View | Where {-not $_.Config.Template}
@Chris
You can add a line after line 7 which reads:
$Details | Add-Member -Name VMID -Value $VM.moref -Membertype NoteProperty
Thanks for the assist, that worked great!
Hi, is there any way I can also see the datastore information?
Hey I am a beginner and I have Powercli working but I cant the script to run. I copied and paste this in the editor and I received a MyCollection error. Please help
@Don Maslanka @Virtu-Al
“I made a modification others may find useful: I added a member named UsedSpace(MB) and calculated the guest vm used disk space (Total – Free).”
Could you provide the code for this? This is exactly what i’m looking to add!
On another note: I just came across these great scripts and definitely appreciate that you are sharing these with everyone.
Hey Al
I am using the guest disk report from your powerpack. I would like to modify it so I can include a filter that allows you to set the % remaining on a disk. For example, I would like to filter for VM’s that only have 10% or less space remaining on a volume.
cheers
You should be able to use the filter feature in PowerGUI to do this, have you tried this already ? – Just above the results in PowerGUI
No, hadn’t tried that to be honest. I’ve been trying out vEcoShell after your excellect demo at VMUG Leeds last week. Can I get a VC host connection from PowerGUI managed computers or do I need to add another VMware powerpack to manage it?
Ok, I can found the filter in vEcoShell. Is there anywhere on the vEcoShell site that lists valid format for regular expressions? Can you pass variables back into the expression, such as ‘Disk1Freespace(MB)’? I need to calculate the ‘free’ space as a % or fraction of the total size. Maybe I’m better off doing it in the code…?
How would i go about adding up the total capacity of all the drives in the VM?
I’m only seeing up to two disks per VM even though I know the VM has more than two attached disks. I’ve looked at the PS code and can’t fathom why this is happening. Is anyone else getting the same result?
KFM, try outputting this to Export-CSV rather than Out-Gridview, it should then work as this is an issue with Out-Gridview.
@Virtu-Al: Thanks mate – that worked a treat!
Is there anyway to output the disks for a server to it’s own line when exporting to csv. So that the it would like like this?
“Name”,”Diskpath”,”DiskCapacity(MB)”,”DiskFreeSpace(MB)”
“vm1″,”C:\”,”20466″,”9241″
“vm1″,”E:\”,”20473″,”20408″,”F:\”,”102398″,”84243″
Sorry that should have been:
“Name”,”Diskpath”,”DiskCapacity(MB)”,”DiskFreeSpace(MB)”
“vm1″,”C:\”,”20466″,”9241″
“vm1″,”E:\”,”20473″,”20408″
“vm1″,”F:\”,”102398″,”84243″
Answered my own question by moving the Export-CSV to outside the braces. So my question is now what would be the easiest way to add the datastore, vmdk path, naa…. #, if it’s thin provisioned and if it’s an RDM into the scritp above? I will keep working and hopefully answer this before you read this.
This script is great. I am curious how I would change it, though, so that the “Name” is the DNS name instead of the name in the VI inventory?
Thanks,
Jeff
First a foremost, thank you for the script. It has been very helpful. I am wondering how I can change the sorting so that my export file is sorted by name rather than number of disks. If I change “Sort-Object -Descending NumDisks” to “Sort-Object Name” the list does sort correctly, but I lose all the fields for any disks that aren’t 0. I’m sure it’s something simple…