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.

2 thoughts on “vSphere Custom Attributes – PowerCLI

    • Author gravatar

      can you also post an example of the csv file looks like ?

      • Author gravatar

        The easiest way to get the format for the csv file, is to use the GUI to create a custom attribute and then go ahead and utilise the export script to export the custom attribute as a CSV. The same format can then be utilised to create new custom attributes or modify existing ones.

Leave a Reply

Your email address will not be published. Required fields are marked *