For the record, especially and also for my personal purpose, I'd like to extend this question and answer a bit.
This is because I'm not a real programmer so I always need some time to figure out how some things work :smileywink: and as I couldn't figure out directly how this works here's what I would like to add in terms of explanation.
Let's say there is a Tag Category that is called Operating System.
Linked to that category are a few tags like:
- Windows 7 (64-bit)
- Windows 8.1 (64-bit)
- Windows 10 (64-bit)
- Windows Server 2012 (64-bit)
- Windows Server 2012 R2 (64-bit)
Also there are (let's say) five VMs that use that Category and link the Operating System Tag according to the installed OS on this VM.
As user Zciklacekic pointed out, the method Remove-TagAssignment by default removes all Tags from a VM rather than a specific one. But if there are other Categories and other Tags related to a VM we of course do not want that.
A statement to remove a (single*) Tag from a single VM would then look like this:
$vm = "Test-VM-Win10"
Get-VM $vm | Get-TagAssignment -Category "Operating System" | Remove-TagAssignment -Confirm:$false
As you can guess this VM has the Tag "Windows 10 (64-bit)" attached to it.
So it gets the VM, reads the Tag(s) that are associated with the Category "Operating System" for this VM and removes all Tags by deleting the reference to the Tag Category.
* Usually an Operating System Category should only be applicable on a VM object and shouldn't have multiple cardinality. It wouldn't make any sense to attach this Tag to a vDS or a ESXi host, would it?! So in case you have other objects with multiple cardinality the above statements removes all assigned Tags with that Category for the selected object or VM.
I'm currently working on an issue with those Tags as I often find VMs that - according to the .vmx configuration file - seem to have an Operating System installed that is not factually installed. So for example the configuration file says "Microsoft Windows Server 2012 (64-bit)" but the VMware Tools report "Microsoft Windows Server 2008 R2 (64-bit)". As this would cause issues when tagging a VM correctly I want to set the Tag according to the VMware Tools information rather than the .vmx configuration file information.
So I'll put the information in a CSV file and do the re-tagging.
As I cannot simply re-tag a VM by applying a new Tag of that same Category I'll precautionary remove the current assiged Tag and set a new Tag afterwards.
So also a big Thanks! from my side, too. And sorry for the elaboration above but I wanted to make it as clear as possible as I stumled upon this issue several times now and also have removed all Tags once which I hope will never happen again ;-) this was a productive environment. Lucky me I had a backup file with the Object <-> Tag info.