VMware Cloud Community
MariusRoma
Expert
Expert

Calculating the size of a folder in a datastore

I need to calculate the space userd inside a folder in a datastore, let's say: [Datastore_1] My_VM

How can I calculate the overall space used by all the files inside the datastore?

Regards

marius

0 Kudos
1 Reply
LucD
Leadership
Leadership

Try something like this

$dsName = 'Datastore_1'

$folderName = 'My_VM'


$ds = Get-Datastore -Name $dsName


New-PSDrive -Location $ds -Name DS -PSProvider VimDatastore -Root '\' | Out-Null

$size = Get-ChildItem -Path "DS:\$folderName" -Recurse | Measure-Object -Property Length -Sum | select -ExpandProperty Sum

$ds | Select @{N='Datastore';E={$ds.Name}},

  @{N='Folder';E={$folderName}},

  @{N='SizeGB';E={[math]::Round($size/1GB,1)}}


Remove-PSDrive -Name DS -Confirm:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos