PowerCLI Get Free Disk Space info from VMs

This script exports all information regarding free disk space on all VMs on your vCenter. This will only work on Powered on VM’s with VMTools installed and running.

Add-PSSnapin VMware.VimAutomation.Core

Connect-VIServer -Server vCenterServer

$MyCollection = @() 

$AllVMs = Get-View -ViewType VirtualMachine | Where {-not $_.Config.Template} 

$SortedVMs = $AllVMs | Select *, @{N="NumDisks";E={@($_.Guest.Disk.Length)}} | Sort-Object -Descending NumDisks


 ($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)) 

$Details | Add-Member -Name "Disk$($DiskNum)Free%" -MemberType NoteProperty -Value ("{0:p2}" -f ($disk.FreeSpace / $disk.Capacity)) 


 } $MyCollection += $Details


$MyCollection | Export-Csv -NoTypeInformation "C:\FreeDiskSpaceInfo.csv" 

Facebooktwittergoogle_pluslinkedinby feather


Leave a Reply

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