vSphere Custom Attributes – PowerCLI

I know that a lot of things have been discussed about Custom Attributes and Tags across various blogs but I thought that I would just share some scripts that I have been using for a particular scenario that other people may also run into.

In the environment I was looking after, we had over 1000 virtual machines which had been deployed over the past few years (most of them just being deployed with templates and no real automation involved).  These virtual machines were not owned by a single person or team but multiple people across multiple teams, a scenario seen at most companies.  In the past we had utilised the ‘Notes’ field of the virtual machine to list some simple information about the virtual machine, such as the owner, email address etc.

This method was not great as all of the information was contained in a single field and therefore searching on the information using scripts etc. proved to be difficult.  Previously, we had also looked at utilising Custom Attributes in earlier versions of vSphere but this proved to be tricky to view or manage as the Custom Attributes did not appear on the summary screen for the VM (especially in the Web Client).

Thankfully, in vSphere 6.5, Custom Attributes can now be fully viewed and managed directly from the vSphere Web Client… meaning that utilising Custom Attributes becomes a real possibility now.


I therefore revisited Custom Attributes and utilised the following scripts to help create the Custom Attribute names, update the virtual machines from a CSV file and also to export the virtual machines including the Custom Attributes.  I have to admit that I am not the original author of these scripts although I have tweaked them and unfortunately I cannot remember who the various authors are (therefore I apologise to the original authors for not being able to give them the full credit they deserve).  I’m sure that these scripts can be improved upon but they do what I need them to do.


Create Custom Attributes

The first script is a simple script to create a number of Virtual Machine Custom Attributes, the ones listed were specific to my requirements but you can adjust these to your requirements.  Please remember to change the ***vCenterName*** to your vCenter name.

connect-viserver ***vCenterName***

New-CustomAttribute -TargetType "VirtualMachine" -Name "VMOwner"
New-CustomAttribute -TargetType "VirtualMachine" -Name "OwnerEmail"
New-CustomAttribute -TargetType "VirtualMachine" -Name "OwnerADLocation"
New-CustomAttribute -TargetType "VirtualMachine" -Name "BusinessReason"
New-CustomAttribute -TargetType "VirtualMachine" -Name "Application"
New-CustomAttribute -TargetType "VirtualMachine" -Name "ServerType"

Disconnect-VIServer -Server ***vCenterName*** -Confirm:$False


Import Custom Attribute Values

The second script takes an input from a csv file and updates the relevant virtual machine Custom Attributes with the additional details from the csv file.  In the csv file there is a column called VMName (housing the full virtual machine name) with additional columns for each of the Custom Attributes.  Once again, remember to change the ***vCenterName*** to your vCenter name and the ***CSVfileLocation*** to the location of the csv file.

connect-viserver ***vCenterName***
$FileList = "***CSVfileLocation***"
$VMList=Import-CSV $FileList
ForEach($Line in $VMList)
 Get-Vm $Line.VMName | Set-Annotation -CustomAttribute "VMOwner" -Value $Line.VMOwner
 Get-Vm $Line.VMName | Set-Annotation -CustomAttribute "OwnerEmail" -Value $Line.OwnerEmail
 Get-Vm $Line.VMName | Set-Annotation -CustomAttribute "OwnerADLocation" -Value $Line.OwnerADLocation
 Get-Vm $Line.VMName | Set-Annotation -CustomAttribute "BusinessReason" -Value $Line.BusinessReason
 Get-Vm $Line.VMName | Set-Annotation -CustomAttribute "Application" -Value $Line.Application
 Get-Vm $Line.VMName | Set-Annotation -CustomAttribute "ServerType" -Value $Line.ServerType
Disconnect-VIServer -Server ***vCenterName*** -Confirm:$False


Export Custom Attributes

The final script scans over the vCenter server and exports all of the virtual machines with their Custom Attributes to a csv file (to allow you to check for any missing data or as a base to update the information before importing it again).  As in the previous scripts, please remember to modify the ***vCenterName*** and ***CSVfileLocation*** with your information.

connect-viserver ***vCenterName*** 

$Report = @()

Get-VM | foreach{

$Summary = "" | Select VMName

$Summary.VMName = $_.Name

$_ | Get-Annotation | foreach{

Add-Member -InputObject $Summary -MemberType NoteProperty -Name $_.Name -Value $_.Value


$Report += $Summary


$Report | Export-Csv -Path ***CSVfileLocation*** -NoTypeInformation

Disconnect-VIServer -Server ***vCenterName*** -Confirm:$False


I hope that this proves to be useful for other people.

About the Author


I have been in IT for the past 15 years and using virtualisation technologies for around the past 8 years. I started, as quite a lot of people do, working with PCs after playing with such iconic systems like the ZX81, ZX Spectrum and then progressing through 386s, 486s, Pentiums etc. After being headhunted at sixth form to work for a small company based around Hertfordshire, UK. I began working with small businesses and gaining a lot of hardware experience. Three years later, after helping to increase the size of the business, I needed to gain exposure to a larger environment to progress my own career. I joined a large manufacturing company around Electronic Test and Measurement which progressed my skills onto more PC work, hardware work and then onto Server Operating Systems. I progressed again onto a consultancy company based in Reading, UK. Initially working as an engineer performing hardware / software installations for larger companies contracted out to the consultancy company, I moved up into a Consultant position continuing my travel across the UK assisting and providing solutions to companies. I finally moved on again to my current position, working back in Hertfordshire, UK. Again working for a large manufacturing company, this time with over 50,000 users worldwide. I am responsible for the datacenter hardware, the storage environment, the vmware environment and also implementing their new Citrix XenApp farm. My days are busy but also productive, its a friendly environment and in my four years of being with the company, I have seen many changes in technology and infrastructure in use within the company. About the site I started this site as I had been thinking of having more of a presence on the web for a while. On a daily basis, I perform tasks and use tools that others may not use or may not think to do and therefore I thought that I would share some of these experiences and tips with others to help with their day to day work. Currently, my main focus of work is around VMware and Veeam Backup & Replication but hopefully as my tasks progress, I’ll be able to share useful bits of information about other areas of IT as well.

Leave a Reply

Your e-mail address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.