Notes of a scripter

PowerCLI to Remove VM Tags

Using VM tags to logically group VMs is awesome, but they can come a time when you need to remove the tags to apply a different one. Using the web console can be very time consuming. PowerCLI to the rescue.

Remove-TagAssignment

Using Remove-TagAssignment is powerful. You can use it to target just a single tag to be removed from a list of VMs.

$Tag = Get-tag "Production"
$VM = Get-VM "VMname"
$VM | Get-TagAssignment $Tag | Remove-TagAssignment

You can also use it to remove tags en masse.

$Tag = Get-tag "Production"
$VMs = Get-VM
Foreach ($VM in $VMs){
     $VM | Get-TagAssignment $Tag | Remove-TagAssignment
}

I have a script that I use internally that will read the tags assigned to one VM, and assign them on a new VM, and removed from the original. This is very handy in the case of replacing a VM that either is broken or it was replaced with a newer one with an updated operating system.

# Move tags from Old VM to New VM
$OldVM = Get-VM "OldVMName"
$NewVM = Get-VM "NewVMName"

# Gathers Tags on Old VM
$VMTags = ($OldVM | Get-TagAssignment).tag.name
foreach ($Tag in $VMTags){
    $NewVM | New-TagAssignment -Tag $(Get-Tag -Name $Tag)
}

# Remove All TAGs from Old VM
$OldVM | Get-TagAssignment | Remove-TagAssignment -Confirm:$False

-Stuart

Exit mobile version