How to get the VM Folder size of datastore using PowerCli

We often get into situation where we need to check the storage space utilization of vmFolder in datastore.

But unfortunately we do not have any option available in vSphere GUI to see the space for vm folders , we need to go inside the folder to see each files . we usually check this when we want to check about orphan files/folder.

Use case scenario:

If you have vSphere replication and SRM and for any instance you remove the machine without un-protecting from SRM and removed the replication. The 2nd copy of vm folder will not be removed . you need to remove them manually.

I have written this function to ease the job to find the folder with utilization details and Last modified details which can help to find the orphan folder also.

You can even generate the report also for same if you would like to save for future reference.

There are some Prerequisite to use this.

We should be connected to only one vCenter at time

Connect-VIServer -Server vcentname -User Username -Password Password

Person should have right to create the CSV file .

function Get-DatastoreFolderSize
{
<#
.NOTES
===========================================================================
Created by: Ankush Sethi
Blog:       www.vmwarecode.com
===========================================================================
.SYNOPSIS
Provide the VMFolder Size Utilization
.DESCRIPTION
Function will provide the vmfolder size in datastore using PowerCli
.PARAMETER Datastore
Enter the Datastore name for which you want to check the utilization
.PARAMETER ExportToCSV
If you want to genetate the report

.EXAMPLE
 Get-DatastoreFolderSize -Datastore (Get-Datastore DSname) 
 Get-Datastore SA-shared-01-ms-remote|Get-DatastoreFolderSize
 Get-Datastore SA-shared-01-ms-remote|Get-DatastoreFolderSize -ExportToCSV:$true
#>

param(
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]
[VMware.VimAutomation.ViCore.Impl.V1.DatastoreManagement.DatastoreImpl]
$Datastore,
[switch]$ExportToCSV
)
Begin
{
$DSobject=New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
$DSfileobject=New-Object VMware.Vim.FileQueryFlags
$DSfileobject.Modification=$true
$DSfileobject.FileSize=$true
$DSfileobject.FileOwner=$true
$DSfileobject.FileType=$true
$DSobject.Details=$DSfileobject
}
Process
        {
Try
{
foreach($as in $Datastore)
        {
$ds=Get-Datastore $as -ErrorAction Stop
        }
}
catch
{
Write-Error -Message "Entered datastore is not found" -ErrorAction Stop
}
foreach($store in $Datastore)
    {
    $dspath="["+$store.name+"]"
$dsview=Get-View -id $store.ExtensionData.browser
$output+=@($dsview.SearchDatastoreSubFolders($dspath,$DSobject)|select Folderpath,
@{N="FolderSize-MB";E={[math]::Round((($_.file|measure -Property Filesize -sum).sum)/1MB,2)}},
@{N="FolderSpace-GB";E={[math]::Round((($_.file|measure -Property FileSize -Sum).sum)/1GB,2)}},
@{N="TotalFiles";E={($_.file|measure -Property FileSize).count}},
@{N="Last-Modified";E={($_.File|Sort-Object -Property Modification -Descending|select -First 1).modification}}
)
    }
        }

End
{
Write-Host "------------------------------------------------------------------------------------------------------------------------------------------------"
$output|FT -AutoSize|Out-Default

Write-Host "------------------------------------------------------------------------------------------------------------------------------------------------"
If($ExportToCSV -eq $true)
{

$path=Get-Location
$name="\VMwareCode_VMfolderReport"+" "+($global:DefaultVIServer).name.Split('.')[0]+".csv"
$Reportname= $path.path+$name
$output|Export-Csv -NoTypeInformation -Path $Reportname
}

}

}

Sample Usage of Function

Get-DatastoreFolderSize -Datastore (Get-Datastore DSName)

Sample usage of Function with Report parameter

Get-DatastoreFolderSize -Datastore (Get-Datastore DSName) -ExportToCSV:$true

Sample usgae with Pipeline:

Get-Datastore DSName|Get-DatastoreFolderSize

Sample Usage with Pipeline and Export Report

Get-Datastore DSname|Get-DatastoreFolderSize -ExportToCSV:$true

It can used for full vCenter level using below reference.

Get-Datastore|Get-DatastoreFolderSize -ExportToCSV:$true

Sample Output of function:

Note: Function will create the CSV file in current directory .In my case it is c:\users\administrator\desktop .

How to Use the Function:

  1. Copy the code from above and paste it into Notepad.
  2. Save it with anyname with extension of .ps1
  3. Open the PowerCli and to go the directory where it is saved using cd command
  4. Run the command . .\FileName.ps1 (Note :There is a space between 2 dots.)
  5. Function is loaded in local session of PowerCli and you are good to use this as cmdlet.

3 responses to “How to get the VM Folder size of datastore using PowerCli”

  1. Thanks for this script, it was usfull in one of my datastore cleanup project, where i used the last modified date to delete the files and folder which was older than 30 days. I was able to recover a lot of valuable storage to the project. Looking for some more useful script…

    Like

  2. Hello Ankush,

    Thanks for the script, it helped me lot to find the orphaned files to delete and much space free space.

    Could you please provide an script to find the list of orphaned file in vSAN datastore.

    Like

Leave a comment

Create a website or blog at WordPress.com