PowerCLI script to list datastore space utilization

Here is a PowerCLI script that lists the datastore space utilization:

Connect to vCenter server

Connect-VIServer -Server -User -Password

Get all the datastores in vCenter

$datastores = Get-Datastore

Loop through each datastore

foreach ($datastore in $datastores) {
# Get the capacity and free space of the datastore
$capacity = $datastore.CapacityGB
$freeSpace = $datastore.FreeSpaceGB

# Calculate the used space in GB
$usedSpace = $capacity - $freeSpace

# Calculate the utilization percentage
$utilization = ($usedSpace / $capacity) * 100

# Write the output to the console
Write-Output "Datastore: $($datastore.Name)"
Write-Output "Capacity: $capacity GB"
Write-Output "Free Space: $freeSpace GB"
Write-Output "Used Space: $usedSpace GB"
Write-Output "Utilization: $utilization %"
Write-Output ""

}

Disconnect from vCenter server

Disconnect-VIServer -Server -Confirm:$false

This script first connects to the vCenter server, then gets all the datastores and loops through each one to calculate the utilization. The utilization is calculated as the used space divided by the capacity, multiplied by 100. The output is written to the console for each datastore, including the datastore name, capacity, free space, used space, and utilization percentage.

Leave a Reply