Create and Assign a Custom Attribute for Virtual Machines in vCenter Using PowerShell


Managing a large number of virtual machines in vCenter can be challenging, especially when you need to keep track of specific information for each VM. Custom attributes can be helpful in such cases, allowing you to store metadata about your VMs. In this blog post, we will walk through a PowerShell script to create and assign a custom attribute called “TechnologyID” to all virtual machines in vCenter, excluding those with names starting with “vCLS” as those are special VMs, managed by DRS.


  1. VMware PowerCLI is installed on the system where the script will be executed.
  2. vCenter Server credentials with sufficient privileges to create and set custom attributes.

PowerShell Script Explanation:

  1. Import the VMware PowerCLI module:
powershellCopy codeImport-Module VMware.PowerCLI

This command imports the VMware PowerCLI module to use its cmdlets in the script.

  1. Connect to the vCenter server:
powershellCopy code$vCenterServer = "your_vcenter_server"
$Username = "your_username"
$Password = "your_password"
Connect-VIServer -Server $vCenterServer -User $Username -Password $Password

Replace your_vcenter_server, your_username, and your_password with your vCenter server address, username, and password, respectively. The Connect-VIServer cmdlet establishes a connection to the vCenter server.

  1. Define the custom attribute name and check if it exists:
powershellCopy code$CustomAttributeName = "TechnologyID"
$customFieldsManager = Get-View (Get-View ServiceInstance).Content.CustomFieldsManager
$CustomAttribute = $customFieldsManager.Field | Where-Object { $_.Name -eq $CustomAttributeName }

We define the custom attribute name as “TechnologyID” and use the Get-View cmdlet to access the vCenter API directly and retrieve the custom fields manager. We then check if the custom attribute already exists.

  1. Create the custom attribute if it doesn’t exist:
powershellCopy codeif (-not $CustomAttribute) {
    $customFieldsManager.AddCustomFieldDef($CustomAttributeName, "VirtualMachine", $null, $null)
    $CustomAttribute = $customFieldsManager.Field | Where-Object { $_.Name -eq $CustomAttributeName }
$CustomAttribute = $CustomAttribute[0]

If the custom attribute doesn’t exist, we create it using the AddCustomFieldDef method. We then retrieve the newly created custom attribute and store the first element in the $CustomAttribute variable.

  1. Iterate through clusters and set the custom attribute value for virtual machines:
powershellCopy code$Clusters = Get-Cluster
foreach ($Cluster in $Clusters) {
    Write-Host "Processing Cluster: $($Cluster.Name)"
    $VMs = Get-VM -Location $Cluster
    foreach ($VM in $VMs) {
        if ($VM.Name -notlike "vCLS*") {
            Write-Host "Setting TechnologyID for VM: $($VM.Name)"
            $vmView = Get-View $VM
            $customFieldsManager.SetField($vmView.MoRef, $CustomAttribute.Key, "")
        } else {
            Write-Host "Skipping VM: $($VM.Name)"

We retrieve all clusters and iterate through them. For each cluster, we retrieve its virtual machines and iterate through them. We use a conditional statement to check if the VM name does not start with “vCLS”. If it doesn’t, we set the custom attribute value for the VM to an empty string. Otherwise, we skip the VM.

  1. Disconnect from the vCenter server:
powershellCopy codeDisconnect-VIServer -Server $vCenterServer -Confirm:$false

Finally, we disconnect from the vCenter server using the Disconnect-VIServer cmdlet, making sure not to prompt for confirmation by setting the -Confirm parameter to $false.


In this blog post, we demonstrated how to create and assign a custom attribute called “TechnologyID” for virtual machines in vCenter using a PowerShell script. This script helps manage and organize VMs by setting custom metadata while excluding specific VMs based on their name pattern. The script can be easily modified to suit your specific requirements, such as using a different attribute name or changing the filtering criteria for the VMs to be processed.

🔥Subscribe to the channel:🔥

🚨Read my blog:

👊My Podcast:

🔥vExpert info:

🛒 VMware EMEA store:

🛒 VMware US store:

🛒 VMware APAC store:

Please leave the comment