Home > Software Development, vSphere API > Set Environment Variables in Guest Operating System on VMware

Set Environment Variables in Guest Operating System on VMware

While reading my last post on reading environment variables from a guest operating system, you may wonder how to set environment variables. Don’t be disappointed if I tell you that there is NO direct support for setting an environment variable.

However, you have a workaround – use a command directly. Unlike reading variables, there is no standard ways to do it for different operating systems. You have to first figure out what type of operating system and then run different commands. For example, if you are targeting Windows family of operating systems, you simply run the following:

Time to learn how to "Google" and manage your VMware and clouds in a fast and secure

C:\Windows\System32\cmd.exe /c setx CLOUD Future -m

The command creates a new environment variable called “CLOUD” and sets its value as “Future.” Note that the setupx command is used instead of the setup command, which sets up the environment variable for a session but not persistent when the session is gone. It’s there by default in Windows 7. For Windows XP, it may require you to install The XP Support Tools which can be included in your XP CD. To play safe, you can just try the above command in the target server first.

The -m switch to the setx command is to specify it’s a system level environment variable; if not there, the command sets an environment variable for the currently logged in user.

Now time to take a look at a sample. To run it, first check out the simple prerequisites in a previous post.

Copyright (c) 2012 Steve Jin, All Rights Reserved.

package org.doublecloud.vi.vmware.guest.samples;

import java.net.URL;

import org.doublecloud.vi.vmware.guest.GuestProcessDirector;

import com.vmware.vim25.mo.Folder;
import com.vmware.vim25.mo.InventoryNavigator;
import com.vmware.vim25.mo.ManagedEntity;
import com.vmware.vim25.mo.ServiceInstance;
import com.vmware.vim25.mo.VirtualMachine;

public class GuestSetEnvVars
  public static void main(String[] args) throws Exception
    ServiceInstance si = new ServiceInstance(new URL(""), "Administrator", "doublecloud.org", true);
    Folder rootFolder = si.getRootFolder();

    ManagedEntity[] mes = new InventoryNavigator(rootFolder).searchManagedEntities("VirtualMachine");
    if (mes == null || mes.length == 0)

    VirtualMachine vm = (VirtualMachine) mes[0];

    System.out.println("guest tool status:" + vm.getGuest().toolsRunningStatus);
    if (!"guestToolsRunning".equals(vm.getGuest().toolsRunningStatus))
      System.out.println("The VMware Tools is not running in the Guest OS on VM: " + vm.getName());

    GuestProcessDirector progDir = new GuestProcessDirector(vm, "Administrator", "vijava");

    progDir.run("C:\\Windows\\System32\\cmd.exe", "/c Setx CLOUD Future -m");

    String[] envVars = progDir.readEnvironmentVariables(new String[] { "CLOUD" });

    for (String envVar : envVars)
      int pos = envVar.indexOf("=");
      String name = envVar.substring(0, pos);
      String value = envVar.substring(pos + 1);
      System.out.println("name: " + name);
      System.out.println("value: " + value);


After the command is executed, you should be able to check its value in a similar way as last post. The cmd.exe will also be gone, so you don’t need to kill it.

With above discussion, you may wonder if you can use commands to read environment variables as well. The answer is yes with a little trick to fetch back the standard output. This has to do with GuestFileDirector API to be discussed in future posts. Stay tuned.

  1. No comments yet.
  1. No trackbacks yet.