In this article we’ll show a simple PowerCLI script to check the amount of free space on VMWare vSphere datastores and detect Thin Provisioning datastores with the total size of virtual machine thin virtual disks (dynamically expanding) exceeding the total size of the datastore. If you have multiple VMWare datastores in your infrastructure, it is easy to use this PowerShell script to monitor the amount of free space and detect datastores with storage overcommitment (the space requirements for thin disks of all VMs are greater than the available space on VMFS datastore). You can use the script to analyze the growth of used space, prior to creating a VM, to find datastores with Thin Provision overcommitment, etc.
To check and display the amount of free space on VMWare datastore, you can use the PowerShell script below (it is supposed that VMWare vSphere PowerCLI module is already installed on your computer):
# Import the PowerCLI module into your PowerShell session
Import-Module VMware.VimAutomation.Core -ErrorAction SilentlyContinue
# Connect to vCenter host
Connect-VIServer mun-vcsa1 -User admin
# Get the list of vCenter darastores
$datastores = Get-Datastore
$ErrorActionPreference = 'SilentlyContinue'
# loop through all available datastores
ForEach ($datastore in $datastores)
# Find the size of the committed space of all thin disks in a datastore (how much space it is required if all vmdk files will grow to the sizes specified in their settings)
$Provision = ([Math]::Round(($datastore.ExtensionData.Summary.Capacity - $datastore.ExtensionData.Summary.FreeSpace + $datastore.ExtensionData.Summary.Uncommitted)/1GB,0))
# Percentage of free space in the datastore
$PerFree = ([math]::Round(($datastore.FreeSpaceGB)/($datastore.CapacityGB)*100,2))
# Percentage of thin disk overcommitment
$PerOvercommit = ([math]::Round($Provision/($datastore.CapacityGB)*100,2))
# Add extra properties to the datastore object
$datastore | Add-Member -type NoteProperty -name PercentsFree -value $PerFree
$datastore | Add-Member -type NoteProperty -name CapacityGb_r -value ([Math]::Round(($datastore.ExtensionData.Summary.Capacity)/1GB,0))
$datastore | Add-Member -type NoteProperty -name FreeSpaceGb_r -value ([Math]::Round(($datastore.ExtensionData.Summary.FreeSpace)/1GB,0))
$datastore | Add-Member -type NoteProperty -name ProvisionedSpaceGb -value $Provision
$datastore | Add-Member -type NoteProperty -name PercentsOvercommit -value $PerOvercommit
# Display the resulting data on VMWare datastores and export the output to a CSV file
$datastores|select-object Name, Type, Datacenter,CapacityGb_r,FreeSpaceGb_r,PercentsFree,ProvisionedSpaceGb,PercentsOvercommit|sort PercentsFree| Export-Csv C:\Reports\VMWareVMFSDatastores.csv -NoTypeInformation
Could not resolve the requested VC server.Additional Information: There was no endpoint listening at https://mun-vcsa1/sdk that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details
it is likely that PowerCLI tries to connect to VCSA via proxy. Run
PowerCLIConfiguration and check if
UseSystemProxy returns. If it is, disable the system proxy for PowerCLI using this command:
Set-PowerCliConfiguration -proxypolicy noproxy
In my example, you can see that the first 5 VMFS datastores have less than 5% of free space left (the green box). There is storage overcommitment on some datastores (the total size of all thin virtual disks in the datastores exceeds their size). If your virtual VM disks start to grow to their maximum size specified in their settings, you may get out of space on your VMFS/NFS/VVOL storages. (Running VMs with thick disks will work as usual, but you will not be able to start new VMs, since there will be no space to create a VSWAP file.) The datastores with the committed space that is larger than the total LUN size are highlighted in yellow.
This PowerShell script will help you to quickly find VMWare datastores with the lack of free space (you can migrate VMs from the datastore using Storage vMotion).