{"id":70203,"date":"2005-03-18T19:02:00","date_gmt":"2005-03-18T19:02:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/18\/how-can-i-create-an-environment-variable-using-a-script\/"},"modified":"2005-03-18T19:02:00","modified_gmt":"2005-03-18T19:02:00","slug":"how-can-i-create-an-environment-variable-using-a-script","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-create-an-environment-variable-using-a-script\/","title":{"rendered":"How Can I Create an Environment Variable Using a Script?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! Can I create an environment variable using a script?<BR><BR>&#8212; OD<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, OD. WMI is a great technology for scripting, but it has its eccentricities. For example, can you create a process using a script? Of course you can; after all, WMI\u2019s Win32_Process class has a Create method. Can you create a service using a script? Of course you can; after all, WMI\u2019s Win32_Service class has a Create method. Can you create an environment variable using a script? Of course you can, despite the fact that the Win32_Environment class <I>doesn\u2019t<\/I> have a Create method. Strange but true!<\/P>\n<P>In fact, here\u2019s a script that creates a new environment variable named TestValue:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set objVariable = objWMIService.Get(&#8220;Win32_Environment&#8221;).SpawnInstance_<\/p>\n<p>objVariable.Name = &#8220;TestValue&#8221;\nobjVariable.UserName = &#8220;&lt;System&gt;&#8221;\nobjVariable.VariableValue = &#8220;This is a test&#8221;\nobjVariable.Put_\n<\/PRE>\n<P>We know: this looks different from most WMI scripts. But don\u2019t worry, we\u2019re going to walk through the code, step-by-step.<\/P>\n<P>Our environment variable script begins the same way most WMI scripts do: by connecting to the WMI service. After connecting to the WMI service you typically call ExecQuery to return a collection of items. However, we\u2019re not going to do that with this script. Instead, we\u2019re going to use the <B>SpawnInstance_<\/B> method to create a \u201cblank\u201d environment variable, one that exists only in memory. (We use the term \u201cblank\u201d because SpawnInstance_ creates an environment variable that has all the properties of an environment variable; however, at the time of creation none of those properties are assigned a value. Hence a \u201cblank\u201d instance.)<\/P>\n<P>This line of code creates an object reference (objVariable) that points to our new environment variable (which, again, exists only in memory when created):<\/P><PRE class=\"codeSample\">Set objVariable = objWMIService.Get(&#8220;Win32_Environment&#8221;).SpawnInstance_\n<\/PRE>\n<P>And, no, that\u2019s not a misprint: the name really <I>is<\/I><B>SpawnInstance_<\/B>, with an underscore coming at the end. You\u2019ll see a lot of underscores before this column is finished.<\/P>\n<P>What we need to do now is fill in the properties of our environment variable. In particular, we need to specify three values:<\/P>\n<P><B>Name<\/B>. This is simply the name of the environment variable. In this sample script we use the clever name <I>TestValue<\/I>.<\/P>\n<P><B>UserName<\/B>. This is the owner of the environment variable. To make this a system variable, set UserName to <B>System<\/B>; to make it a user-specific environment variable, set UserName to the user account name (for example, <B>fabrikam\\kenmyer<\/B>).<\/P>\n<P><B>VariableValue<\/B>. The assigned value of the environment variable. Once again we\u2019ve gone overboard with our cleverness, assigning TestValue the value <I>This is a test<\/I>.<\/P>\n<P>And, yes, that\u2019s why we Scripting Guys get paid the big bucks.<\/P>\n<P>With the three property values assigned, we then call the <B>Put_<\/B> method, which actually writes the new environment variable to the operating system. Mission accomplished.<\/P>\n<P>What do you mean, \u201cAre you sure?\u201d If you don\u2019t believe us, a quick way to verify creation is to use a script similar to this one. This script uses a WQL query to select all environment variables with the name <B>TestValue<\/B>, then echoes the property values of that variable to the screen: <\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Environment Where Name = &#8216;TestValue'&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo &#8220;Name: &#8221; &amp; objItem.Name\n    Wscript.Echo &#8220;User Name: &#8221; &amp; objItem.UserName\n    Wscript.Echo &#8220;Variable Value: &#8221; &amp; objItem.VariableValue\n    Wscript.Echo\nNext\n<\/PRE>\n<P>Of course, what\u2019s the fun of having a brand-new, custom-made environment variable if you never get to play with it? If you want to change the value of your new environment variable, simply connect to that variable (using the same WQL query we just showed you) and assign a new value to the <B>VariableValue <\/B>property. Call the <B>Put_<\/B> method to write the changes to the operating system, and your environment variable will take on the new value. For example, this sample script changes the value of TestValue to <I>New value<\/I>:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Environment Where Name = &#8216;TestValue'&#8221;)<\/p>\n<p>For Each objItem in colItems\n    objItem.VariableValue = &#8220;New value&#8221;\n    objItem.Put_\nNext\n<\/PRE>\n<P>Child\u2019s play.<\/P>\n<P>But what if you want to <I>delete<\/I> your environment variable? (Yes, even after all the work you did to create it.) That\u2019s easy enough: we just take the same basic approach we used when modifying the value of the environment variable. This time, however, we don\u2019t assign a new value to the VariableValue property; instead we simply call the <B>Delete_<\/B> method to delete the variable:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\nSet objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery _\n    (&#8220;Select * from Win32_Environment Where Name = &#8216;TestValue'&#8221;)<\/p>\n<p>For Each objItem in colItems\n    objItem.Delete_\nNext\n<\/PRE>\n<P>Again, note the underscore on the end of the method name: <B>Delete_<\/B>.<\/P>\n<P>Of course, there\u2019s still one question left unanswered: how did we know that the Win32_Environment class supported the SpawnInstance_ and Delete_ methods? In this case, we used Wbemtest.exe. Call up Wbemtest and take a look at the Win32_Environment class:<\/P><IMG border=\"0\" alt=\"Wbemtest\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/wbemtest.jpg\" width=\"350\" height=\"327\"> \n<P><BR>If you look at the very top of the screen (under the <B>Qualifiers<\/B> heading) you\u2019ll see the <B>CreateBy<\/B> and <B>DeleteBy<\/B> qualifiers, followed by the methods used to create a new instance (<B>PutInstance<\/B>) and delete an existing instance (<B>DeleteInstance<\/B>). <I>That\u2019s<\/I> how we could tell that Win32_Environment supported SpawnInstance_ and Delete_. <\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Can I create an environment variable using a script?&#8212; OD Hey, OD. WMI is a great technology for scripting, but it has its eccentricities. For example, can you create a process using a script? Of course you can; after all, WMI\u2019s Win32_Process class has a Create method. Can you create a service [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,3,4,5,6],"class_list":["post-70203","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Can I create an environment variable using a script?&#8212; OD Hey, OD. WMI is a great technology for scripting, but it has its eccentricities. For example, can you create a process using a script? Of course you can; after all, WMI\u2019s Win32_Process class has a Create method. Can you create a service [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70203"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70203\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}