Find Virtual Machine Configuration File Path using Powercli

3

Today's post is titled Find Virtual Machine Configuration File Path using Powercli. I was recently working with a customer whose task was to find out how to perform a sanity check of the datastores within the vSphere environment.

The reason behind this task was that many of the Virtual Machines were removed from inventory and the customer was not sure on how to clear up the datastores.

So, he was looking for an easy way to find out the list of VMs that were currently registered to the vCenter Server and the path to the Virtual Machine configuration file so that he doesn't delete the files that were associated with the VMs already registered.

Normally, you would go to the Virtual Machine settings and from the options tab to find the path of the Virtual Machine Configuration file.

Now doing this for one or two VMs is fine, but performing this step manually for all the VMs in the inventory can be a painful task.

Hence, I decided to a small function that will help you select a Virtual Machine and provide the Display Name and the path to its configuration file.

If you like an easy way to this, then below is the one-liner which will list all the VMs with the config file path.

Get-VM | select Name,@{E={$_.ExtensionData.Config.Files.VmPathName};L="VM Path"}

Find Virtual Machine Configuration File Path using Powercli

You can find the function that I have written below.

Function Get-VMConfigFile {
    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory=$true,
            ValueFromPipeline=$true,
            ValueFromPipelineByPropertyName=$true)]
            [String[]]$VMName
    )
Get-VM $VMName | select Name,@{E={$_.ExtensionData.Config.Files.VmPathName};L="VM Path"}
}

So, there are a couple of ways that you can run this function. The first one is to directly call the function and provide the parameter which accepts the VM name.

Get-VMConfigFile VCSA65-A

Find Virtual Machine Configuration File Path using Powercli

The second is to pipe the output of Get-VM cmdlet to the function.

Get-VM VCSA65-B | Get-VMConfigFile

Find Virtual Machine Configuration File Path using Powercli

I hope this has been informative and thank you for reading!

Share.

About Author

I am Adil Arif, working as a Senior Technical Support Engineer at Rubrik as well as an independent blogger and founder of Enterprise Daddy. In my current role, I am supporting infrastructure related to Windows and VMware datacenters.

3 Comments

  1. PS D:\juan\script\decomm\MoveVMS> .\Get-VMConfigFile.ps1 w2k3x32-vm031
    At D:\juan\script\decomm\MoveVMS\Get-VMConfigFile.ps1:20 char:80
    + Get-VM $VMName | select Name,@{E={$_.ExtensionData.Config.Files.VmPathName};L=”V …
    + ~
    Unexpected token ‘VM’ in expression or statement.
    At D:\juan\script\decomm\MoveVMS\Get-VMConfigFile.ps1:25 char:59
    + $dsname = $vmxfile.split(” “)[0].TrimStart(“[“).TrimEnd(“]”)
    + ~~
    The string is missing the terminator: “.
    + CategoryInfo : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : UnexpectedToke

  2. Hi Adil,

    I also needed to get the vmx path for a bunch of VMs. I couldn’t get your one liner to work:
    Get-VM | select Name,@{E={$_.ExtensionData.Config.Files.VmPathName};L=”VM Path”}

    But as it looked like it should work I modified it to this, which now works:
    Get-VM | select Name,@{N=”VMX Path”;E={$_.ExtensionData.Config.Files.VmPathName}}

    Hope this helps someone.

    Cheers,
    Gerg

Leave A Reply