Skip to content
Menu
Tech Automation Blog
  • About Author
  • Contact
Tech Automation Blog

List VMware VM Templates in VCenter

Posted on February 5, 2020February 27, 2022

If you want to generate a quick inventory of all VMware Virtual Machine Templates created in your vCenter environment you can use PowerCLI command-lets and pull that data quickly.

Today I am sharing a script which helped me a lot as I have several vCenters to support globally and each one has several Templates for different OS Platforms/OS versions etc. With this script I can run it against all vCenters and generate a csv file with required data. Usually, I review those to check for any old or obsolete Template, that I need to remove and create a new one or in case if someone has created a new Template, which I should be aware of.

I will be using vCenter Name and Output file path as input in this script and assign values for those. As I always say, feel free to update this script and pass those as parameters.

Here is the complete script. Let me know if this is helpful.

$oFile = "<Some Path>\allTemplateDetails.csv" #Output file to store script run result
$vCenterName = "<myvcenter>.<somedomain>.<com>" # vCenter FQDN

# Check if Output file exists.If exists, delete it and create a new file. You can set the file name unique with date-time stamp if you want generate unique file every time
if(Test-Path $oFile){
    Remove-Item -Path $oFile -Force
}
"vCenter Name,Template Name,Template Path" | Out-file $oFile -Append -Encoding ASCII

connect-viserver $vCenterName #Connect to the vCenter Server.If you have multiple vCenters, enter a for-loop to loop through them

# Filter out view result only for Templates
Get-View -ViewType virtualmachine -Filter @{'Config.Template'='TRUE'} | ForEach-Object{
    $templateName = $_.Name
    $currentPath = Get-View $_.Parent
    $templatePath = $_.Name
    #traverse through the path and find out the logical path of the template in vCenter
    do{
        $parentPath = $currentPath
        if($parentPath.Name -ne "vm"){
           $templatePath = $parentPath.Name +"\"+ $templatePath
        }
        $currentPath = Get-View $currentPath.Parent
    }while($currentPath.Parent -ne $null)
    "$($vCenterName),$($templateName),$($templatePath)" | Out-File $oFile -Append -Encoding ASCII
}
disconnect-viserver $vCenterName # disconnect from vCenter

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on LinkedIn (Opens in new window) LinkedIn
May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  
« May    

Recent Posts

  • Monitor and alert Azure Service Health issues May 5, 2020
  • AWS IAM User access review May 3, 2020
  • Integrate Azure Security Center with Event Hub April 28, 2020
  • Add Tags to Azure Subscription April 24, 2020
  • Automate Azure billing report in Excel March 6, 2020

Categories

©2025 Tech Automation Blog | Powered by SuperbThemes