PowerCLI

 View Only
  • 1.  Renaming files on a datastore

    Posted Nov 24, 2025 02:13 AM

    Is there a way to programmatically rename a file on a datastore with PowerCLI?   I know that I can copy files to and from using Copy-DatastoreItem, and I know that I could just copy down, rename it locally, then copy it back up, but that is far too time consuming.

    Short of opening an SSH session to a host, is there another way to do this?

    If people are asking for a use case, let's just say that I need to rename an .iso file that is there for reasons.



    -------------------------------------------


  • 2.  RE: Renaming files on a datastore

    Posted Nov 25, 2025 01:17 PM
    I second that question. I have a use-case where I detach VMDKs using the powercli SDK, ssh in, to use the old vmdk commands to 'move' the vmdk to another vm's folder, and then use ssh again to rename the file, and then attach. It would be sweet if there was a powershell way to accomplish this without ssh. I already have the code for detach/attach, so its just the move/rename I'm lacking.




  • 3.  RE: Renaming files on a datastore

    Posted Nov 25, 2025 02:01 PM

    Keep in mind that this is all processing through your workstation so, in the case of moving a VMDK from one datastore to another, you're essentially copying it to your local machine, then copying it back to the new location, so it can increase the time it takes considerably.

    You would probably be better off establishing an SSH session directly to a host and then passing the commands through that way:

    $hostname = "myhost.mydomain.com"
    
    #Query for SSH credentials (You could save this to a credential file to read back again later for automation)
    $myhostcred = Get-Credential -Message "Enter credentials for host $hostname"
    
    # Get the host information
    $myHost = Get-VMHost -Name $hostname
    
    #Check the host to see if SSH is enabled and enable it if not.
    $mySSH = Get-VMHostService -VMHost $myHost | where {$_.key -eq "TSM-SSH"}
    $mySSHstatus = $mySSH.Running
    if ($mySSHStatus -eq $false) {
        $mySSH = Start-VMHostService -HostService $mySSH
        $mySSHstatus = $mySSH.Running
        if ($mySSHstatus -eq $false) {
            Write-Host "Unable to enable SSH on the host"
            exit
        }
    }
    
    # Establish an SSH session to the host and run the command.
    $sshcmd = "mv /vmfs/volumes/datastore1/oldfolder/mydrive.vmdk /vmfs/volumes/datastore2/newfolder/mydrive.vmdk"
    $ssh = New-SSHSession -ComputerName $hostname -Credential $myhostcred -AcceptKey -KeepAliveInterval 5
    $result = Invoke-SSHCommand -SessionId $ssh.SessionId -Command $sshcmd -Timeout 30
    
    # Write the resulting console output to the screen.
    Write-Host $result.Output
    
    # Close the SSH Session quietly
    $nooutput = Remove-SSHSession -SessionID $ssh.SessionId

    The only downside to this approach is needing to request and/or store the credential information, but it will be far more time efficient than trying to do it through a PSDrive.

    -------------------------------------------



  • 4.  RE: Renaming files on a datastore
    Best Answer

    Posted Nov 25, 2025 01:44 PM
    Edited by Charlie Silverman Nov 25, 2025 02:03 PM

    As it turns out, I did actually already find information about how to do this but I had misinterpreted something so it didn't work for me before.

    There is a method for accessing a datastore via PowerCLI as a regular drive so you can use any of the methods that you would usually use (Remove-Item, Rename-Item, etc.).   This is done using a PSDrive,

    Sample code for this would be:

    Connect-ViServer vcenter.mydomain.com
    $datastore = Get-Datastore mydatastore
    New-PSDrive -Location $datastore -Name myds -PSProvider VimDatastore -Root '\'

    At this point, you can access the datastore as any other drive at myds:\, so to rename a file on the datastore, you could do something like:

    Rename-Item -Path myds:\ISOs\myimage.iso -NewName mynewimage.iso

    -------------------------------------------



  • 5.  RE: Renaming files on a datastore

    Posted Nov 25, 2025 01:47 PM
    Sweet, thanks so much




  • 6.  RE: Renaming files on a datastore

    Posted Dec 25, 2025 12:11 PM

    Yes, use the data store browser service available through the vcenter connection.content object. The PSDrive method is slow and clunky 

    -------------------------------------------