Copying files to and from vSphere datastore volumes using PowerCLI
search cancel

Copying files to and from vSphere datastore volumes using PowerCLI

book

Article ID: 311572

calendar_today

Updated On:

Products

VMware vCenter Server VMware vSphere ESXi

Issue/Introduction

This article provides a method to copy files to and from a Windows workstation or server to a vCenter Server datastore (VMFS or NFS) using VMware vSphere PowerCLI. For more information on PowerCLI, including installation and usage, see the vSphere PowerCLI documentation.

Note: Automation or scheduling of this method is outside the scope of this article. For information on automation and scheduling, see the Microsoft Windows Task Scheduler documentation.


Environment

VMware ESX 4.1.x
VMware ESX Server 3.5.x
VMware vCenter Server 4.0.x
VMware VirtualCenter 2.5.x
VMware ESXi 4.1.x Installable
VMware ESXi 4.0.x Embedded
VMware vCenter Server 4.1.x
VMware vSphere PowerCLI 4.0
VMware ESXi 3.5.x Embedded
VMware ESXi 4.1.x Embedded
VMware ESX 4.0.x
VMware ESXi 3.5.x Installable
VMware ESXi 4.0.x Installable
VMware vSphere PowerCLI 4.1

Resolution

The PowerCLI Datastore Provider (VimDatastore) works with the PowerShell drive functionality to expose access to files and directories on vSphere datastores. You can use the default vmstore: or vmstores: inventory drives, or create custom drives with the New-PSDrive commandlet. Files and directories can be copied using the Copy-DatastoreItem commandlet. For more information, see the The Datastore Provider section of the vSphere PowerCLI Administration Guide.

To create a new custom PowerShell drive for a vSphere datastore:

  1. Connect to an ESX/ESXi host or vCenter Server using PowerCLI:

    Connect-VIServer -Server ServerNameOrIPAddress

  2. Get a datastore object:

    $datastore = Get-Datastore "MyDatastoreName"

  3. Create a new PowerShell drive, such as ds:, that maps to $datastore:

    New-PSDrive -Location $datastore -Name ds -PSProvider VimDatastore -Root "\"

To locate a datastore file or directory on a PowerShell drive:

  1. Connect to an ESX/ESXi host or vCenter Server using PowerCLI:

    Connect-VIServer -Server ServerNameOrIPAddress

  2. Change locations into the PowerShell drive using the Set-Location command:

    Set-Location PowerShellDriveName:\

    For example:

    • Set-Location ds:\
    • Set-Location vmstore:\

    For more information on usage, see the PowerShell Set-Location commandlet documentation.


  3. Get a listing of files and directories using the Get-ChildItem command:

    ds:\> Get-ChildItem

    You see an output similar to:

    LastWriteTime Type Length Name
    ------------- ---- ------ ----
    8/31/2010 12:51 PM Folder virtualmachine1
    6/14/2011 11:34 AM Folder virtualmachine1
    6/8/2011 8:13 PM Folder virtualmachine1



    For more information on usage, see the PowerShell Get-ChildItem commandlet documentation.

  4. Change into a subfolder using the Set-Location command.

    Set-Location "<DirectoryName>"

    For example:

    Set-Location "virtualmachine1"

  5. Repeat steps 2 and 3 until the desired file or directory is located.

To copy a file to or from a vSphere Datastore and the local filesystem of the PowerCLI host:

  1. Connect to an ESX/ESXi host or vCenter Server or using PowerCLI:

    Connect-VIServer -Server ServerNameOrIPAddress

  2. Locate the source file or directory.

  3. Copy the source file or directory to a destination using the Copy-DatastoreItem command:

    Copy-DatastoreItem -Item SourceDatastoreItem(s) [-Destination DestinationLocation] [-Force] [-Recurse] [-Confirm]

    For example:

    • Copy-DatastoreItem -Item ds:\virtualmachine1\virtualmachine1.vmx -Destination c:\virtualmachine1\
    • Copy-DatastoreItem -Item c:\virtualmachine2\* -Destination ds:\virtualmachine2\

    For more information, see the PowerCLI Copy-DatastoreItem commandlet documentation.

    Notes:
    • Files cannot be copied directly between vSphere datastores in different vCenter Servers using Copy-DatastoreItem. Copy the files to the PowerCLI host's local filesystem temporarily, and then copy to the destination.
    • The file paths after -Item and -Destination are case sensitive.
Note: The preceding links were correct as of March 31, 2015. If you find a link is broken, provide feedback and a VMware employee will update the link.

Additional Information

This method is not the only way to copy files to and from vSphere datastores. For other options, see the Product Documentation for your version of vSphere and the vSphere Command-Line Interface documentation.

Copia de archivos hasta y desde los volúmenes del almacén de datos vSphere mediante PowerCLI
PowerCLI を使用した vSphere データストアとの間のファイルのコピー
使用 PowerCLI 将文件复制到 vSphere 数据存储卷或从 vSphere 数据存储卷中复制文件