Get Basic VMWare DataStore Statistics via PowerShell PowerCLI
Big thanks to LucD, and this post which is essentially the entire solution: https://communities.vmware.com/t5/VMware-PowerCLI-Discussions/PowerCLI-script-to-get-DatastoreCluster-Datastores-and-the-size/td-p/1301104
Problem: I need stats on datastores. Specifically, i need to find which DataStores are over-provisioned compared to the VM’s assigned to it.
Solution:
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory=$True,Position=0,HelpMessage="The script will output to .CSV datastore statistics for the provided vCenter Server hostname or FQDN")] | |
[string]$vCenter | |
) | |
Import-Module VMware.VimAutomation.Core | Out-Null | |
try { | |
Connect-VIServer -Server $vCenter -Protocol https -Force -ErrorVariable err -ErrorAction SilentlyContinue | Out-Null | |
} catch { | |
Out-Null | |
} finally { | |
if ($err.Count -eq 0) { | |
Write-Output "Fetching DataStore statistics..." | |
$datastores = get-datastore | |
$report = foreach ($store in $datastores) { | |
$store | Select @{N='Datacenter';E={$_.Datacenter.Name}}, | |
@{N='DSCluster';E={Get-DatastoreCluster -Datastore $_ | Select -ExpandProperty Name}}, | |
Name,CapacityGB,@{N='FreespaceGB';E={[math]::Round($_.FreespaceGB,2)}}, | |
@{N='VMCount';E={$_.ExtensionData.VM.Count}}, | |
@{N='ProvisionedSpaceGB';E={[math]::Round(($_.ExtensionData.Summary.Capacity - $_.Extensiondata.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/1GB,2)}}, | |
@{N='UnCommittedGB';E={[math]::Round($_.ExtensionData.Summary.Uncommitted/1GB,2)}}, | |
@{N='FreeGB/CapacityGB %';E={[math]::Round(($_.FreespaceGB/$_.CapacityGB)*100,2 -f 000.00)}}, | |
@{N='ProvisionedGB/CapacityGB %';E={[math]::Round(($_.ExtensionData.Summary.Capacity - $_.Extensiondata.Summary.FreeSpace + $_.ExtensionData.Summary.Uncommitted)/$_.CapacityGB/1GB*100,2 -f 000.00)}} | |
} | |
$report | Sort-Object -Property 'ProvisionedGB/CapacityGB %' | Export-Csv datastore-stats.CSV -NoTypeInformation -UseCulture | |
ls datastore-stats.CSV | |
disconnect-viserver -Confirm:$false | |
} else { | |
Write-Output "failed to connect to ${vCenter}. Exiting." | |
Exit | |
} | |
} | |
The script will output to .CSV file sorted by ProvisionedGB/CapacityGB percentage.
As such, you may see which datastore are heavily over-provisioned (in the sense of datastore larger than necessary).
Of course, you may also sort my other columns as needed.
Please consider crypto tipping:



