PowerCLI Script – Report datastore free space Percentage

I hope we are all busy with evaluating vSphere 6.5 and its cool new features. Let me take short break from vSphere 6.5 and will write article about PowerCLI script to Report of  datastore free space Percentage . In most of the vSphere enterprise environment, They would have set an standard datastore free space Percentage that should always be maintained across all datastore as enterprise standard. I would always recommend 10% to 20 %  free space should always be kept free to accommodate VM operations such as Snapshots, Memory Increase which needs swap space in datastore and also for best practices. You may be having hundred’s to thousands of datatstores in your virtualized infrastructure.  Manually validating all datastores Free space percentage is painfull. This powercli script helps you to get the report of datastores along with free space Percentage. This helps you to quickly address the datastore which is having less free datastore space as per your organization standard.

PowerCLI Script – Report datastore free space Percentage

#      Script Name	:   DataStoreFreeSpace-Percentage.ps1
#      Purpose		:   Get the report of datastores which has less than 20% free space.
#      Date		:   24-11-2016	# - Initial version
#                   	:  
#      Author		:   www.VMwareArena.com
If(!(Get-PSSnapin | Where {$_.Name -Eq "VMware.VimAutomation.Core"}))
	Add-PSSnapin VMware.VimAutomation.Core

$VCServer = Read-Host 'Enter VC Server name'
$vcUSERNAME = Read-Host 'Enter user name'
$vcPassword = Read-Host 'Enter password' -AsSecureString
$vccredential = New-Object System.Management.Automation.PSCredential ($vcusername, $vcPassword)

$LogFile = "DataStoreInfo_" + (Get-Date -UFormat "%d-%b-%Y-%H-%M") + ".csv" 

Write-Host "Connecting to $VCServer..." -Foregroundcolor "Yellow" -NoNewLine
$connection = Connect-VIServer -Server $VCServer -Cred $vccredential -ErrorAction SilentlyContinue -WarningAction 0 | Out-Null
If($? -Eq $True)

	Write-Host "Connected" -Foregroundcolor "Green" 
	$Results = @()
	$Result = Get-Datastore | Select @{N="DataStoreName";E={$_.Name}},@{N="Percentage Free Space(%)";E={[math]::Round(($_.FreeSpaceGB)/($_.CapacityGB)*100,2)}} | Where {$_."Percentage(<20%)" -le 20}
	$Result | Export-Csv -NoTypeInformation $LogFile
	Write-Host "Error in Connecting to $VIServer; Try Again with correct user name & password!" -Foregroundcolor "Red" 
Disconnect-VIServer * -Confirm:$false


How to Execute the Script?

Copy and Paste the above Script and save it in the Notepad. Name the script as “DataStoreFreeSpace-Percentage.ps1″

  1. Execute the Script in PowerCLI “.\DataStoreFreeSpace-Percentage.ps1
  2. Input  vCenter Server Name to execute the script to get the report free space Percentage of datastore
  3. Enter the Username with administrative credentials on vCenter Server
  4. Enter the password for the above entered Username
  5. Hit Enter to execute the script and pull the report.

free space Percentage of datastore

Script Output

Datastore free space Percentage report will be exported and saved in the Microsoft Excel output file with the filename “DataStoreInfo_Today_Day-time.CSV”  under the same directory where the PowerCLI Script “DataStoreFreeSpace-Percentage.ps1”  is located.

datastore free space Percentage

I hope script will be useful for you to get the datastore free space percentage to monitor the space usage of your datastores. Thanks for Reading !!! Be social and share it in social media, if you feel worth sharing it.