PowerCLI for vSphere Tags and Custom Attributes: Metadata Automation Across Your Inventory

Introduction

Tags and custom attributes bring metadata structure to your VMware environment. Whether you’re classifying workloads, enforcing policies, or generating reports, PowerCLI enables full automation of tag and attribute management.

In this article, you will learn how to:

  • Create and organize tag categories
  • Assign tags to VMs, hosts, and other objects
  • Query tags for filtering and automation
  • Manage custom attributes and annotations
  • Export tag metadata for documentation

My Personal Repository on GitHub

VMware Repository on GitHub


Understanding Tags vs Custom Attributes

FeatureTagsCustom Attributes
ScopeFlexible across object typesTypically for VMs only
OrganizationCategory-basedKey-value attached directly
Use CasesBackup policies, app tiering, auditBuild info, cost centers, notes
Automation ReadyYes (Get-Tag, New-TagAssignment)Yes (Get-CustomAttribute)

Tag Management with PowerCLI

Create a Tag Category

New-TagCategory -Name "ApplicationType" -Cardinality Single -EntityType VirtualMachine

Create Tags Inside the Category

New-Tag -Name "WebApp" -Category "ApplicationType"
New-Tag -Name "Database" -Category "ApplicationType"

Assign a Tag to a VM

$vm = Get-VM -Name "SQL01"
$tag = Get-Tag -Name "Database"
New-TagAssignment -Entity $vm -Tag $tag

List Tags for a VM

Get-VM -Name "SQL01" | Get-TagAssignment

Bulk Tag Assignment Using Filters

Tag all VMs in a folder or matching pattern.

$tag = Get-Tag -Name "WebApp"
Get-Folder -Name "Web Servers" | Get-VM | New-TagAssignment -Tag $tag

Or based on guest OS:

Get-VM | Where-Object {$_.Guest.OSFullName -like "*Ubuntu*"} | New-TagAssignment -Tag "Linux"

Exporting Tag Assignments to CSV

Get-VM | ForEach-Object {
$vm = $_
Get-TagAssignment -Entity $vm | Select-Object @{N="VM";E={$vm.Name}}, Tag, Category
} | Export-Csv -Path "C:\Reports\VM_Tags.csv" -NoTypeInformation

Managing Custom Attributes

List Existing Attributes

Get-CustomAttribute

Create a New Attribute

New-CustomAttribute -Name "Owner" -TargetType VirtualMachine

Set Value on a VM

Set-Annotation -Entity (Get-VM -Name "SQL01") -CustomAttribute "Owner" -Value "TeamA"

Report Custom Attribute Values

Get-VM | Select Name, @{N="Owner";E={($_ | Get-Annotation | Where-Object {$_.Name -eq "Owner"}).Value}}

Diagram: Tag and Attribute Automation


Use Case: Backup Classification by Tag

You can tag VMs as Daily, Weekly, or Excluded and use backup tools to process workloads accordingly.

Assign tag:

New-TagAssignment -Entity (Get-VM -Name "AppServer01") -Tag "Daily"

Backup solutions like Veeam, Commvault, and Rubrik can query vCenter tags to dynamically build protection groups.


Troubleshooting

IssueFix
Tag assignment failsVerify category entity type matches object (e.g., VM vs host)
Duplicate tags or categoriesUse Get-Tag and Get-TagCategory to confirm uniqueness
Cannot read custom attribute valueUse Get-Annotation and filter on .Name explicitly
Attribute not visible in vCenter UIRestart vSphere Client or verify access permissions

What’s Next

Next article will focus on:

  • Generating compliance and health reports using PowerCLI
  • Scheduling reports for VM state, orphaned snapshots, and configuration drift

Leave a Reply

Discover more from Digital Thought Disruption

Subscribe now to keep reading and get access to the full archive.

Continue reading