Add multiple VMware datastores using PowerCLI

The other day I had to add 60+ new VMware datastores, rather than doing this manually using the vSphere web client wizard, I used a basic PowerCLI script (in a foreach loop) to create the datastores in one big bang!

Before carrying out these steps make sure new LUNs are presented to the ESXi host.  And make a note of the LUN id of first new LUN you created.  

NOTE – You can acquire the LUN id by analyzing “Runtime Name” of LUN.  Or alternatively the storage array may have this information also.  

First run the following script:    

Get-ScsiLun -VMHost "HOST-NAME" -LunType disk | Select RuntimeName,CanonicalName,CapacityGB | Sort-Object -Property {$_.RuntimeName.Split(‘:’)[0],[int]($_.RuntimeName.Split(‘:’)[1].TrimStart(‘C’))},{[int]($_.RuntimeName.Split(‘:’)[2].TrimStart(‘T’))},{[int]($_.RuntimeName.Split(‘:’)[3].TrimStart(‘L’))} | export-csv c:\temp\ALL-LUNs.csv

This script will produce a CSV file called ALL-LUNs.csv which contains all the Canonical Names of LUNs the ESXi host has access to in ascending “LUN id” order.  

Open CSV and navigate to the row which contains the LUN id of the first new LUN you created.  Delete all rows above it.  Re-save CSV as NEW-LUNs.csv.  


Open new CSV and create a new column called DatastoreNames.  Populate the DataStoreNames column manually.  The names you use in this column purely depend on your datastore naming convention.  Re-save CSV as NEW-DATASTORES.csv.  

Now run the following script against CSV file to add datastores in one big bang!  

$CSVFile = "C:\temp\New-Datastores.csv"
$LUNs = Import-CSV $CSVFile
Foreach ($LUN in $LUNs) {
New-Datastore -VMHost "HOST-NAME" -Name $LUN.DatastoreName -Path $LUN.CanonicalName -Vmfs

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.