VMware Cloud Community
m0t0rh3ad
Contributor
Contributor
Jump to solution

PowerCLI delete unused TAGs

Hello.

Anybody have powercli script for tags cleaning: find tags that no attached to any object and delete them?

Tags (2)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this.
Remove the WhatIf switches when you are sure the output is correct.

When you don't want to remove the TagCategories without Tags, remove the last part (starting with Get-TagCategory).

$tableUsed = @()

Get-TagAssignment |

   ForEach-Object -Process {

   $tableUsed += "$($_.Tag.Category)-$($_.Tag.Name)"

   }


Get-Tag |

   ForEach-Object -Process {

   if ($tableUsed -notcontains "$($_.Category)-$($_.Name)") {

   Remove-Tag -Tag $_ -Confirm:$false -WhatIf

   }

   }

Get-TagCategory |

   ForEach-Object -Process {

   if (-not (Get-Tag -Category $_)) {

   Remove-TagCategory -Category $_ -Confirm:$false -WhatIf

   }

   }


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

View solution in original post

1 Reply
LucD
Leadership
Leadership
Jump to solution

Try something like this.
Remove the WhatIf switches when you are sure the output is correct.

When you don't want to remove the TagCategories without Tags, remove the last part (starting with Get-TagCategory).

$tableUsed = @()

Get-TagAssignment |

   ForEach-Object -Process {

   $tableUsed += "$($_.Tag.Category)-$($_.Tag.Name)"

   }


Get-Tag |

   ForEach-Object -Process {

   if ($tableUsed -notcontains "$($_.Category)-$($_.Name)") {

   Remove-Tag -Tag $_ -Confirm:$false -WhatIf

   }

   }

Get-TagCategory |

   ForEach-Object -Process {

   if (-not (Get-Tag -Category $_)) {

   Remove-TagCategory -Category $_ -Confirm:$false -WhatIf

   }

   }


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