VMware Cloud Community
Hetfield84
Enthusiast
Enthusiast

Get a list of tags that are not assigned to an object in vCenter

Hi,

I'm trying to figure out how to write a script that will give me a list of all tags that are not assigned to an object (in other words not in use). I looked at the Get-Tag and Get-TagAssignments but don't see any switches allowing me to do this.

0 Kudos
1 Reply
LucD
Leadership
Leadership

Provided you don't have Tags with the same name in different Categories, you could do something like this

$tab = @{}

Get-Tag |

ForEach-Object -Process {

    if(-not $tab.ContainsKey($_.Name)){

        $tab.Add($_.Name,$_.Category)

    }

}

Get-TagAssignment | Group-Object -Property {$_.Tag.Name} |

ForEach-Object -Process {

    $tab.Remove($_.Name)

}


$tab.GetEnumerator() | ForEach-Object -Process {

    Write-Host "$($_.Value)/$($_.Name) not used"

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos