Add Virtual Machine Templates (.VMTX) to Inventory using PowerShell

Here is a script that will scan a datastore for VM templates (.VMTX) and add them to inventory. This is a modified version of the Add Virtual Machines (.VMX) to Inventory script.

You must have VMware PowerCLI installed in order to have the cmdlets required for PowerShell to run the script.

Add VMTX (Virtual Machine Templates) to Inventory from a Datastore:

Let’s see it in action!

I want to scan my “Synology LUN” datastore and add any virtual machine templates it finds into the in the “_Templates” folder in the cluster named “Lab Cluster 01″.

VMTX Powershell Script - Environment Variables

I prefer to run my scripts out of PowerShell ISE as I feel it gives me more control. I updated the variables (highlighted in yellow) and ran it:

VMTX Powershell Script - Ran from ICE

In the Tasks pane of the vSphere Client if you see failed tasks the script found templates that were already in your inventory. The templates that were not in my inventory are now in the “_Templates” folder:

VMTX - vCenter after script ran

Add VMX (Virtual Machines) to Inventory from a Datastore Cluster:

The $Datastore variable can be changed to scan a Datastore Cluster instead of specific ones:

I updated the highlighted variables:

VMTX Powershell Script - Ran from ICE for Storage Cluster

I hope this helps speed up the process if you find yourself in a situation needing to scan and add multiple VM templates to inventory!

2 thoughts on “Add Virtual Machine Templates (.VMTX) to Inventory using PowerShell”

  1. Great scripts.
    Did a little modification in the last section of Templates script, for my Templates to appear without .vmtx name in inventory:

    foreach ($vmtxFile in $unregistered)
     {
    $TemplateName = $vmtxFile.Name -replace “.vmtx”
     New-Template -Name $TemplateName -TemplateFilePath $vmtxFile.DatastoreFullPath -VMHost $ESXHost -Location $VMFolder -RunAsync
     }

    Thanks.

    Reply

Leave a Reply