{"id":51003,"date":"2010-03-13T00:01:00","date_gmt":"2010-03-13T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/03\/13\/hey-scripting-guy-weekend-scripter-accessing-environmental-variables\/"},"modified":"2010-03-13T00:01:00","modified_gmt":"2010-03-13T00:01:00","slug":"hey-scripting-guy-weekend-scripter-accessing-environmental-variables","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-weekend-scripter-accessing-environmental-variables\/","title":{"rendered":"Hey, Scripting Guy! Weekend Scripter: Accessing Environmental Variables"},"content":{"rendered":"<h2>\n<p><img decoding=\"async\" alt=\"Bookmark and Share\" src=\"http:\/\/s7.addthis.com\/static\/btn\/v2\/lg-share-en.gif\" width=\"125\" height=\"16\"><br \/><font size=\"2\">&nbsp;<\/font><\/p>\n<p class=\"MsoNormal\">Microsoft Scripting Guy Ed Wilson here. I spent the day at <a href=\"http:\/\/www.sqlsaturday.com\/\"><font face=\"Segoe\">SQL Saturday<\/font><\/a> in Charlotte, NC. I could not resist the opportunity to network with that many rabid Windows PowerShell addicts. I mean, anyone who gives up their Saturday to come to a Microsoft office and sit in training sessions all day is dedicated. Definitely my type of geek. It was a wonderful day, and I was able to meet some tweeps (Twitter followers) in real life. <\/p>\n<p class=\"MsoNormal\">During one of the breaks, I was talking to a scripter from Charleston, South Carolina. Charleston is a beautiful town with lots of old houses, as seen in this picture I took a few years ago when I was in town to teach a VBScript class. <\/p>\n<p class=\"Fig-Graphic\"><img decoding=\"async\" title=\"Photograph taken in Charleston, South Carolina\" alt=\"Photograph taken in Charleston, South Carolina\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2010\/march\/hey0313\/wes-03-13-10-01.jpg\" width=\"600\" height=\"450\"><\/p>\n<p class=\"MsoNormal\">He was asking about accessing environmental variables from within Windows PowerShell. I told him there were several ways, but he did not seem to believe me, and thought it was a rather difficult task. <\/p>\n<p class=\"MsoNormal\">One way to access the environmental variables is to use the Windows PowerShell environmental drive. To do this, you use the <b>$env:<\/b> variable, which represents the environmental drive. You then access the specific variable by using its name. For example, the value of the <b>os<\/b> environmental variable is seen here:<\/p>\n<p class=\"MsoNormal\"><span>PS C:&gt; $env:os<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>Windows_NT<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\"><span>PS C:&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">Another way to access environmental variables is by using WMI and querying the <b>Win32_Environment<\/b> WMI class. This is shown here:<\/p>\n<p class=\"MsoNormal\"><span>PS C:&gt; get-wmiobject win32_environment | where-object { $_.name -eq &#8216;os&#8217; }<\/p>\n<p>VariableValue<span>&nbsp;&nbsp;&nbsp;&nbsp; <\/span><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/span>Name<br \/>&#8212;&#8212;&#8212;&#8212;-<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>&#8212;-<br \/>Windows_NT<span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>OS<\/p>\n<p>PS C:&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">You can use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/6s7w15a0(VS.85).aspx\"><font face=\"Segoe\">wshEnvironment<\/font><\/a> object that is obtained from the <b>environment<\/b> property of the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aew9yb99(VS.85).aspx\"><font face=\"Segoe\">WshShell<\/font><\/a> object:<\/p>\n<p class=\"MsoNormal\"><span>PS C:&gt; $wshShell = New-Object -ComObject wscript.shell<br \/>PS C:&gt; $wshShell.Environment().item(&#8220;os&#8221;)<br \/>Windows_NT<br \/>PS C:&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">In addition to working directly with the <b>wshEnvironment<\/b> object, the <b>WshShell<\/b> object also contains a method called <b>ExpandEnvironmentStrings<\/b> that can be used to translate the value of an environmental variable. This is seen here:<\/p>\n<p class=\"MsoNormal\"><span>PS C:&gt; $wshShell = New-Object -ComObject wscript.shell<br \/>PS C:&gt; $wshShell.ExpandEnvironmentStrings(&#8220;%os%&#8221;)<br \/>Windows_NT<br \/>PS C:&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">One of my favorite tricks is to run the command interpreter inside Windows PowerShell. By running the command interpreter, you have access to the <b>set<\/b> command. You can use <b>set<\/b> to query the current value of an environmental variable as well as change the value. This is seen here (remember, that you will need to type <b>exit<\/b> to leave the cmd prompt and return to Windows PowerShell):<\/p>\n<p class=\"MsoNormal\"><span>PS C:&gt; cmd<br \/>Microsoft Windows [Version 6.1.7600]<br \/>Copyright (c) 2009 Microsoft Corporation.<span>&nbsp; <\/span>All rights reserved.<\/p>\n<p>C:&gt;set os<br \/>OS=Windows_NT<\/p>\n<p>C:&gt;exit<br \/>PS C:&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">One last way to obtain the value of an environmental variable is to use the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.environment.aspx\"><font face=\"Segoe\">System.Environment<\/font><\/a> .NET Framework class. The <b>System.Environment<\/b> .NET Framework class has the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.environment.getenvironmentvariable.aspx\"><font face=\"Segoe\">GetEnvironmentVariable<\/font><\/a> static method that accepts an environmental variable as an argument. This is seen here:<\/p>\n<p class=\"MsoNormal\"><span>PS C:&gt; [environment]::GetEnvironmentVariable(&#8220;os&#8221;)<br \/>Windows_NT<br \/>PS C:&gt;<\/p>\n<p><\/span><\/p>\n<p class=\"MsoNormal\">Well, there are a half dozen different ways of accessing the same environmental variable. I am sure that there are more, but right now, it is getting kind of late (it has been a rather long day), and I cannot think of any more ways at this tired moment. If you think of any more ways to access environmental variables, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> and I will collect the new ways, and talk about them.<\/p>\n<p class=\"MsoNormal\">If you want to know exactly what we will be looking at tomorrow, follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><font face=\"Segoe\">Twitter<\/font><\/a> or <span><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><font face=\"Segoe\">Facebook<\/font><\/a><\/span>. If you have any questions, send e-mail to us at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\"><font face=\"Segoe\">scripter@microsoft.com<\/font><\/a> or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><font face=\"Segoe\">Official Scripting Guys Forum<\/font><\/a>. See you tomorrow. Until then, peace.<\/p>\n<p class=\"MsoNormal\"><span><\/p>\n<p>&nbsp;<\/p>\n<p><\/span><\/p>\n<p><b><span>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/p>\n<p><\/span><\/b><\/p>\n<p><b><span><\/span><\/b>&nbsp;<\/p>\n<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. I spent the day at SQL Saturday in Charlotte, NC. I could not resist the opportunity to network with that many rabid Windows PowerShell addicts. I mean, anyone who gives up their Saturday to come to a Microsoft office and sit in training sessions all day is dedicated. [&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":[86,31,3,61,45],"class_list":["post-51003","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-basic-information","tag-operating-system","tag-scripting-guy","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Microsoft Scripting Guy Ed Wilson here. I spent the day at SQL Saturday in Charlotte, NC. I could not resist the opportunity to network with that many rabid Windows PowerShell addicts. I mean, anyone who gives up their Saturday to come to a Microsoft office and sit in training sessions all day is dedicated. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51003","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=51003"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/51003\/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=51003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=51003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=51003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}