Win32_Environment class
Applies to: desktop apps only
The Win32_Environment WMI class represents an environment or system environment setting on a Windows computer system. Querying this class returns environment variables found in:
HKLM\System\CurrentControlSet\Control\Sessionmanager\Environment
andHKEY_USERS\<user>\Environment
The following syntax is simplified from Managed Object Format (MOF) code and includes all of the inherited properties. Properties are listed in alphabetic order, not MOF order.
Syntax
class Win32_Environment : CIM_SystemResource
{
string Caption;
string Description;
datetime InstallDate;
string Name;
string Status;
boolean SystemVariable;
string UserName;
string VariableValue;
};
Members
The Win32_Environment class has these types of members:
Properties
The Win32_Environment class has these properties.
- Caption
-
- Data type: string
- Access type: Read-only
Short description of the object.
- Description
-
- Data type: string
- Access type: Read-only
Description of the object.
- InstallDate
-
- Data type: datetime
- Access type: Read-only
Object was installed. This property does not require a value to indicate that the object is installed.
- Name
-
- Data type: string
- Access type: Read/write
- Qualifiers: Key
Character string that specifies the name of a Windows-based environment variable. By specifying the name of a variable that does not yet exist, an application creates a new environment variable.
Example: "Path"
- Status
-
- Data type: string
- Access type: Read-only
Current status of the object. Various operational and nonoperational statuses can be defined. Operational statuses include: "OK", "Degraded", and "Pred Fail" (an element, such as a SMART-enabled hard disk drive, may be functioning properly but predicting a failure in the near future). Nonoperational statuses include: "Error", "Starting", "Stopping", and "Service". The latter, "Service", could apply during mirror-resilvering of a disk, reload of a user permissions list, or other administrative work. Not all such work is online, yet the managed element is neither "OK" nor in one of the other states.
The values are:
- "OK"
- "Error"
- "Degraded"
- "Unknown"
- "Pred Fail"
- "Starting"
- "Stopping"
- "Service"
- "Stressed"
- "NonRecover"
- "No Contact"
- "Lost Comm"
- SystemVariable
-
- Data type: boolean
- Access type: Read-only
Indicates whether the variable is a system variable. A system variable is set by the operating system, and is independent from user environment settings.
- UserName
-
Name of the owner of the environment setting. It is set to <SYSTEM> for settings that are specific to the Windows-based system (as opposed to a specific user) and <DEFAULT> for default user settings.
Example: "JSmith"
- VariableValue
-
- Data type: string
- Access type: Read/write
Placeholder variable of a Windows-based environment variable. Information like the file system directory can change from computer to computer. The operating system substitutes placeholders for these.
Example: "%SystemRoot%"
Remarks
The Win32_Environment class is derived from CIM_SystemResource. You can use this class to find the paths of special folders such as the System folder or Program files on a remote machine. Some examples are: windir, systemroot, programfiles, and userprofile. Win32_environment basically returns what can be found in:
HKLM\System\CurrentControlSet\Control\Sessionmanager\Environment
andHKEY_USERS\<user>\Environment
The calling process that uses this class must have the SE_RESTORE_NAME privilege on the computer in which the registry resides. For example, if you enumerate this class on the local computer, the account under which your application runs must have this privilege. For more information, see Executing Privileged Operations.
Examples
For script code examples, see WMI Tasks for Scripts and Applications and the TechNet ScriptCenter Script Repository.
For C++ code examples, see WMI C++ Application Examples.
The following VBScript code example enumerates the environment variables on the local computer.
strComputer = "." Set objWMIService = GetObject( _ "winmgmts:\\" & strComputer & "\root\cimv2") Set colVar = objWMIService.ExecQuery( _ "Select * from Win32_Environment") For Each objVar in colVar Wscript.Echo "Description: " & objVar.Description _ & VBNewLine & "Name: " & objVar.Name _ & VBNewLine & "System Variable: " _ & objVar.SystemVariable & VBNewLine _ & "User Name: " & objVar.UserName & VBNewLine _ & "Variable Value: " & objVar.VariableValue Next
The following VBScript code example changes an environment variable named BUILD_TYPE to a value input by the user. The script assumes that the BUILD_TYPE variable already exists. If it does not exist then the script ends. The input value is checked: it must be either "Build1", "Build2", or "Build3", and no other value is accepted. The VBScript UCase function makes the input case-insensitive. If what is typed in is not one of the three acceptable values, the script ends.
On Error Resume Next strComputer = "." Set objWMIService = GetObject( _ "winmgmts:\\" & strComputer & "\root\cimv2") Set colItems = objWMIService.ExecQuery( _ "Select * from Win32_Environment") Found = False For Each objItem in colItems If objItem.Name = "BUILD_TYPE" Then Found = True Change = UCase(InputBox("BUILD_TYPE currently = " _ & objItem.VariableValue & VBNewLine _ & "Change options are Build1, Build2, Build3 ")) If UCase(Change) = "BUILD1" OR _ Change = "BUILD2" OR _ Change = "BUILD3" Then objItem.VariableValue = Change objItem.Put_ WScript.Echo "BUILD_TYPE changed to " _ & objItem.VariableValue Else WScript.Echo "No input or unacceptable input." _ & " No change to BUILD_TYPE" End If End If Next If Found = False Then WScript.Echo "User-defined environment" _ & " variable BUILD_TYPE not found." End If
Requirements
|
Minimum supported client | Windows 2000 Professional |
|---|---|
|
Minimum supported server | Windows 2000 Server |
|
Namespace |
\root\CIMV2 |
|
MOF |
|
|
DLL |
|
See also
Send comments about this topic to Microsoft
Build date: 3/9/2012