Using PowerShell to View Datastore and Virtual Machine Space Usage Information in VMware

Hi All,

There may be times when you need to view VMware datastore usage information such as capacity, free space, or how much space each virtual machine is using. In this article I will demonstrate how to gather this information formatted and listed on your screen or in a csv file. This will save you from having to spend all day obtaining this information from the vCenter client manually. To do anything with PowerShell in VMware you will need to install VMware vSphere PowerCLI. You will then need to open PowerCLI and connect to your VMWare vCenter server by typing “Connect-VIServer vCenterServerName”.

To start, I will show you how to get list of the VMware Datastores themselves using the “Get-Datastore” cmdlet. To start, simply type the following command and press “Enter”:

Get-Datastore

This command above will list all datastores giving you the free space and capacity for each. The output will look similar to the following information:

Name                         FreeSpaceGB       CapacityGB
 —-                               ———–                  ———-
DATASTORE1            6.618                      850.000
DATASTORE2           88.378                   850.000
DATASTORE3          126.165                   850.000
DATASTORE4           71.557                    850.000
 

Now that we have a list of datastores, we want to see how much space each virtual machine on a particular datastore is using. In the following example I use the “Get-Datastore” cmdlet and pipe it to the “Get-VM” cmdlet to see how much space on DATASTORE1 each virtual machine is using. Type the following command replacing DATASTORE1 with the name of your datastore.

Get-Datastore DATASTORE1 | Get-VM | select name,usedspacegb

The above command will list the name of each virtual machine and the used space for each in gigabytes.

If you would like to get a list of all the properties you can select in addition to or instead of “name” and “usedspacegb” you can use can pipe it to the Get-Member cmdlet to list them. Type the following command replacing DATASTORE1 with the name of your datastore.

Get-Datastore DATASTORE1 | Get-VM | Get-Member

Next, I would like to export this data to a csv file for later review. In the following command I will export the space info to a csv file name datastore.csv located in the c:\output directory.

Get-Datastore DATASTORE1 | Get-VM | select name,usedspacegb | Export-Csv c:\output\datastore.csv -NoTypeInformation

Lastly, if I wanted to export all the Virtual Machines properties for a particular datastore I could type the following command:

Get-Datastore DATASTORE1 | Get-VM | Export-Csv c:\output\datastore.csv -NoTypeInformation

For information on creating a full virtual machine inventory in VMware, please my other article Using PowerShell to create a Virtual Machine Inventory in VMware and Export it to a CSV File.

Hope this helps. For more information out the “Get-Datastore” cmdlet and the “Get-VM” cmdlets, you can simply type “Get-Help Get-Datastore” or “Get-Help Get-VM”. If you have any questions or feedback, please leave a comment.

Related Links:

Using PowerShell to get a list Virtual Machine Snapshots in VMware ESXi 4.1

Using PowerShell to Gracefully Shut Down Multiple Virtual Machines in VMware

Using PowerShell to Remove Virtual Machine Snapshots in VMware ESXi 4.1

Using PowerShell to create a Virtual Machine Inventory in VMware and Export it to a CSV File

Using PowerShell to Create Virtual Machine Snapshots in VMware

Using PowerShell to View  Virtual Machine Disks (VMDK) Information in VMware and Export to a CSV File

Share Button
Author: BC

BC is a Pipe2text.com contributor and has been an IT Professional for 15 years. He currently works as a Sr. Systems Engineer at a private firm. He enjoys working with Active Directory, Exchange and VMware among other technologies.


Leave a Reply

Your email address will not be published. Required fields are marked *