In this article, I will go over the basics of managing the VMware datastore with PowerCLI cmdlets.

In a VMware vSphere environment, a datastore is the basic building block for storage. You can use a few different types of datastores (such as NFS, iSCSI, or Fibre Channel), but they all do the same thing: they provide a means to store virtual machine data as well as other data.

What is a datastore

What is a datastore

For one-off tasks such as creating or removing a datastore in vSphere, a GUI is a fine choice, as it can easily guide the user. Although when it comes to bulk changes—for instance, adding 20 NFS datastores to each ESXi host—PowerCLI can save sysadmins hours of time and is a much better tool if you are working in a large environment.

For the sake of simplicities, I will be using NFS as my datastore type.

Creating a datastore ^

In PowerCLI, I must specify the VMhost, the name for the datastore, the path, and the NFS host when creating a new datastore:

New-Datastore -Nfs -VMHost TestVMhost -Name "NewDatastore" -Path "/vol/NewDataStore" ‑NfsHost

As I alluded to earlier, typically you are going to want to add a datastore to all ESXi hosts at once. To create this datastore on all VMhosts in the cluster, I could use this command:

New-Datastore -Nfs -VMHost (Get-VMHost | Select-Object -ExpandProperty Name) -Name "NewDatastore" -Path "/vol/NewDataStore" -NfsHost

Viewing datastore information ^

To view information on any datastore in vSphere, you can run the Get-Datastore cmdlet. Piping the output to Select-Object * shows us some additional information such as authentication method, path, type of datastore, current state, and capacity.

C:\> Get-Datastore NewDatastore | Select-Object *

RemoteHost                     : {}
RemotePath                     : NewDatastore
UserName                       :
AuthenticationMethod           : AUTH_SYS
FileSystemVersion              : 3.0
DatacenterId                   : Datacenter-datacenter-27
Datacenter                     : C4
ParentFolderId                 : Folder-group-s30
ParentFolder                   : datastore
DatastoreBrowserPath           : vmstores:\vcenter@443\Datacenter\NewDatastore
FreeSpaceMB                    : 1522509
CapacityMB                     : 13946060
Accessible                     : True
Type                           : NFS
StorageIOControlEnabled        : False
CongestionThresholdMillisecond : 30
State                          : Available
ExtensionData                  : VMware.Vim.Datastore
CapacityGB                     : 13619.19921875
FreeSpaceGB                    : 1486.8251953125
Name                           : itg_vmstore
Id                             : Datastore-datastore-77644
Uid                            : /VIServer=ntdomain\hermes@seaway:443/Datastore=Datastore-datastore-77644/
Client                         : VMware.VimAutomation.ViCore.Impl.V1.VimClient

To filter through data, we can pipe output to Where-Object as well, such as this example below, which only shows NFS datastores:

Get-Datastore | Where-Object {$_.Type -eq 'NFS'}

Removing datastores ^

There are only a few parameters for Remove-Datastore, -Datastore, -VMHost, and -Server.

These are quite self-explanatory. For instance, if you are decommissioning storage and want to remove a datastore from all hosts, you can run:

Remove-Datastore -Datastore NewDatastore -VMHost (Get-VMHost | Select-Object ‑ExpandPropery Name)

Moving items between datastores ^

One of the great functionalities of vSphere is the ability to seamlessly move not only virtual machines, but also other files between datastores. This comes in handy especially during maintenance windows and storage migrations among other tasks.

To move virtual machines between datastores (vMotion), the Move-VM cmdlet is useful:

Move-VM -VM 'TestVM' -Datastore 'NewDatastore'

We all know vMotion has been around forever, but what if I want to move an ISO (or any type of file) between datastores?

When you connect to vCenter with Connect-ViServer, it creates a PSDrive in your PowerShell session that gives you filesystem-like access to your datastores with the VimDatastore provider. To access them, we just use the path.

In this example, I have a file "test.iso" I want to upload to my "NewDataStore" datastore.

Subscribe to 4sysops newsletter!

Copy-DatastoreItem -Item C:\Stuff\test.iso -Destination 'vmstore:\DataCenter\NewDataStore\'

Using a GUI to manage vSphere is perhaps an easier way to use the system, but if you really want to excel in automating large-scale tasks, there is no better tool than PowerCLI combined with some good old PowerShell.

  1. sadheesh 4 years ago

    Hi Dan,

    Copy Item getting below error, tried with multiple PowerCLI versions but no luck. Any help will be appreciated.


    PS C:\> Copy-DatastoreItem C:\temp\local2Ds.txt $ds.DatastoreBrowserPath -Recurse
    Copy-DatastoreItem : Upload of file ‘C:\temp\local2Ds.txt’ failed. Error message: The remote server returned an error: (400) Bad Request.
    At line:1 char:1
    + Copy-DatastoreItem C:\temp\local2Ds.txt $ds.DatastoreBrowserPath -Rec …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Copy-DatastoreItem], VimException
    + FullyQualifiedErrorId : Client20_StorageServiceImpl_UploadFile_UploadFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.CopyDatastoreItem


    PS C:\> get-childitem –recurse (get-datastore “USPNRDHPR002”).datastorebrowserpath |where-Object{$ -like ‘server*.txt’}|Copy-datastoreItem -Destination c:\temp -PassThru
    Copy-datastoreItem : 6/22/2018 7:29:46 AM VimDatastore Download of file
    ‘’ failed. Error message: The remote server
    returned an error: (404) Not Found.
    At line:1 char:119
    + … like ‘server*.txt’}|Copy-datastoreItem -Destination c:\temp -PassThru
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Copy-DatastoreItem], VimException
    + FullyQualifiedErrorId : Client20_StorageServiceImpl_DownloadFile_DownloadFailed,VMware.VimAutomation.ViCore.Cmdlets.Commands.CopyDatastoreIt

    Copy-datastoreItem : Object reference not set to an instance of an object.
    At line:1 char:119
    + … like ‘server*.txt’}|Copy-datastoreItem -Destination c:\temp -PassThru
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Copy-DatastoreItem], NullReferenceException
    + FullyQualifiedErrorId : System.NullReferenceException,VMware.VimAutomation.ViCore.Cmdlets.Commands.CopyDatastoreItem


    Sadheesh S

  2. sadheesh 4 years ago

    Faced similar Issue and below solution helped to fix.
    1. C:\ProgramData\VMware\VMware VirtualCenter
    2. edit vpxa.cfg
    3. Edit below line False to True

Leave a reply

Your email address will not be published.


© 4sysops 2006 - 2022


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account