{"id":64463,"date":"2007-07-13T00:43:00","date_gmt":"2007-07-13T00:43:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/07\/13\/how-can-i-use-windows-powershell-to-retrieve-environment-variables-and-special-folder-paths\/"},"modified":"2007-07-13T00:43:00","modified_gmt":"2007-07-13T00:43:00","slug":"how-can-i-use-windows-powershell-to-retrieve-environment-variables-and-special-folder-paths","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-use-windows-powershell-to-retrieve-environment-variables-and-special-folder-paths\/","title":{"rendered":"How Can I Use Windows PowerShell to Retrieve Environment Variables and Special Folder Paths?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I use Windows PowerShell to retrieve the value of environment variables and the path to a special folder?<BR><BR>&#8212; SB <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, SB. Well, it\u2019s Thursday, which means that the Scripting Guy who writes this column is currently in Walla Walla, WA, which, as <A href=\"http:\/\/looneytunes.warnerbros.com\/stars_of_the_show\/daffy_duck\/daffy_story.html\" target=\"_blank\"><B>Daffy Duck<\/B><\/A> fans all know, is the home of the Wishy Washy Washing Machine Company; various divisions of the Acme Corporation; and who-knows-what-else. <\/P>\n<P>Oh, we know what else: Walla Walla was also immortalized in the song lyrics \u201cOo ee oo-ah-ah, ting tang walla walla bing bang.\u201d Walla Walla is even featured in the Wikipedia entry on <A href=\"http:\/\/en.wikipedia.org\/wiki\/Inherently_funny_word\" target=\"_blank\"><B>inherently funny words<\/B><\/A>. Pretty good for a town of 30,000 people, wouldn\u2019t you say?<\/P>\n<P>Of course, the Scripting Guy who writes this column isn\u2019t spending his time in Walla Walla sitting around doing nothing. (That\u2019s what he does when he\u2019s at work.) Instead, he\u2019s busy doing all sorts of things, including<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Writing this column.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Getting ready for Game 1 of the Walla Walla Invitational Baseball Tournament.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>Watching the thermometer creep past the 750 degree mark.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>OK, so maybe it\u2019s not <I>quite<\/I> 750 degrees here, at least not 750 degrees Fahrenheit. However, the temperature <I>is<\/I> expected to climb above 100 degrees, and some of this Script Guy\u2019s fellow baseball parents are already slathering themselves with sunscreen, drinking tons of water, and complaining about the heat.<\/P>\n<P>And that\u2019s a bad sign; after all, most of them haven\u2019t even gotten out of their air-conditioned cars yet.<\/P>\n<P>Having grown up in the nearby Tri-Cities, however, the Scripting Guy who writes this column isn\u2019t going to be deterred by a little heat; after all, he once played a baseball game in 114 degree heat. (And, oddly enough, had to walk 12 miles uphill, through the snow, in order to get to the game.) He\u2019s not complaining about the heat, taking water intravenously, or even wearing sunscreen; on top of that, he\u2019s about to answer a question about retrieving environment variable values and special folder paths using Windows PowerShell.<\/P>\n<P>Yes, he <I>is<\/I> quite a man, isn\u2019t he?<\/P>\n<P>For starters, let\u2019s see how we can retrieve the values of an environment variable. As SB noted, in VBScript you can retrieve the value of the <I>%logonserver%<\/I> environment variable by using code similar to this:<\/P><PRE class=\"codeSample\">Set oShell = CreateObject(&#8220;Wscript.Shell&#8221;)<\/p>\n<p>Set WshSysEnv = oShell.Environment(&#8220;PROCESS&#8221;)\nsLogonServer = WshSysEnv(&#8220;LOGONSERVER&#8221;)\n<\/PRE>\n<P>Pretty straightforward: we create an instance of the <B>Wscript.Shell<\/B> object, grab the collection of process environment variables, then store the value of the logonserver in a variable named sLogonServer. So how do we get this same information using Windows PowerShell? Why, like this, of course:<\/P><PRE class=\"codeSample\">$a = $env:logonserver\n<\/PRE>\n<P>Yes, that <I>is<\/I> easy, isn\u2019t it? As it turns out, Windows PowerShell has an environment provider that makes it a snap to access information about all the environment variables on a computer. As you can see, all we have to do is specify the <B>$env<\/B> variable followed by a colon and the name of the environment variable we\u2019re interested in. Want to store the value of the <I>%username%<\/I> environment variable in a variable named $a? Then use this syntax:<\/P><PRE class=\"codeSample\">$a = $env:username\n<\/PRE>\n<P>Or maybe you\u2019d like to know the computer name:<\/P><PRE class=\"codeSample\">$a = $env:computername\n<\/PRE>\n<P>If it wasn\u2019t for the baseball game, we could sit here and retrieve environmental variables all day long.<\/P>\n<P>You know, that\u2019s a good question: what if you don\u2019t <I>know<\/I> which environment variables are available on a computer? Fortunately that\u2019s no big deal: to view all the environment variables and their values use this command (note that, in this case, we leave the $ off and reference the <B>env:<\/B> drive instead):<\/P><PRE class=\"codeSample\">dir env:\n<\/PRE>\n<P>Try this command in Windows PowerShell and see what you get back.<\/P>\n<P>OK, now what about the path to special folders? As SB pointed out, this is a pretty easy task to perform in VBScript; for example, the following VBScript code retrieves the path to the user\u2019s desktop folder:<\/P><PRE class=\"codeSample\">Set oShell = CreateObject(&#8220;Wscript.Shell&#8221;)<\/p>\n<p>Set oSFolders = oShell.SpecialFolders\nsDesktop = oSFolders(&#8220;Desktop&#8221;)\n<\/PRE>\n<P>As you can probably see for yourself, the preceding script creates an instance of the <B>Wscript.Shell<\/B> object, creates an object reference to the <B>SpecialFolders<\/B> collection, then retrieves the path to the desktop folder. We could employ this exact same approach using Windows PowerShell; alternatively, we could use the .NET Framework\u2019s System.Environment class (and the GetFolderPath method) to retrieve the path to the desktop folder. All things considered, however, we decided that it\u2019s easier (and more powerful) to use the the <B>Shell.Application<\/B> object to retrieve special folder paths:<\/P><PRE class=\"codeSample\">$a = New-Object \u2013com Shell.Application\n$b = ($a.namespace(0x10)).Self.Path\n<\/PRE>\n<P>OK, granted, this might not <I>look<\/I> all that easy. With that in mind, let\u2019s see if we can explain how it works. In line 1 we simply use the <B>New-Object<\/B> cmdlet to create an instance of a new COM object (hence the <B>\u2013com<\/B> parameter). And what new COM object are we creating? You got it: Shell.Application.<\/P>\n<P>Actually, that was pretty easy; line 2 is a different story. But don\u2019t panic: line 2\u2019s bark is far worse than its bite. In fact, line 2 only looks complicated because we\u2019re actually performing a couple different tasks with a single line of code. To begin with, we\u2019re taking the object reference to the Shell.Application object ($a) and using the <B>Namespace<\/B> method to bind to a particular folder. That\u2019s what this portion of the script is for:<\/P><PRE class=\"codeSample\">($a.namespace(0x10))\n<\/PRE>\n<P>What\u2019s the 0x10 for? That\u2019s a hexadecimal value that represents the Desktop folder. And how did we <I>know<\/I> that 0x10 represents the Desktop folder? To tell you the truth, we didn\u2019t; that\u2019s why we popped over to the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/desktop\/special\/default.mspx\"><B>Script Repository<\/B><\/A> and took a peek at the special folder scripts. As it turns out, the script titled <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/desktop\/special\/dmspvb24.mspx\"><B>List Items in the Desktop Folder<\/B><\/A> looks like this:<\/P><PRE class=\"codeSample\">Const DESKTOP = &amp;H10&amp;<\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(DESKTOP)\nSet objFolderItem = objFolder.Self\nWscript.Echo objFolderItem.Path<\/p>\n<p>Set colItems = objFolder.Items\nFor Each objItem in colItems\n    Wscript.Echo objItem.Name\nNext\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EYG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Note<\/B>. Do you want to use Windows PowerShell to list all the items in the desktop folder? Then just add a third line to your script: <B>Get-ChildItem $b<\/B>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Notice the first line of code, where we assign the hexadecimal value &amp;H10&amp; to a constant named Desktop. The &amp;H10&amp; (which uses VBScript syntax for hexadecimal values) is the value that represents the Desktop folder; all we have to do is convert that to <B>0x10<\/B>, the Windows PowerShell syntax for hexadecimal values. Suppose we were interested in the Internet Cookies folder instead. Well, the VBScript script for working with that folder includes this line of code:<\/P><PRE class=\"codeSample\">Const COOKIES = &amp;H21&amp;\n<\/PRE>\n<P>Now, guess what the PowerShell equivalent to &amp;H21&amp; is? That\u2019s right: 0x21.<\/P>\n<P>Etc., etc.<\/P>\n<P>You might also have noticed that we placed parentheses around the call to the Namespace method:<\/P><PRE class=\"codeSample\">$b = ($a.namespace(0x10)).Self.Path\n<\/PRE>\n<P>Why did we do that? That\u2019s easy: Windows PowerShell always takes care of anything in parentheses before it performs any other tasks. That means that Windows PowerShell is going to first use the Namespace method to bind to the Desktop folder; it\u2019s only after that task is complete that PowerShell retrieves the value of the <B>Self.Path<\/B> property, which just happens to be the path to the Desktop folder. The parentheses simply make sure that Windows PowerShell first gets the folder and only then tries to retrieve the folder path.<\/P>\n<P>Granted, this can be a tad bit confusing. But remember, all you have to do is replace the 0x10 with the appropriate value and you can easily bind to any special folder on the computer.<\/P>\n<P>We hope that helps, SB; the truth is, it\u2019s going to have to, because it\u2019s almost time for the game to start. You know, watching high school baseball in Walla Walla is like a trip down memory lane for the Scripting Guy who writes this column; back when he was in high school he actually played a few games in Walla Walla. For example, when he played here as a sophomore the umpires were prisoners on work release from the Walla Walla State Penitentiary. Were these inmates <I>good<\/I> umpires? Let\u2019s put it this way: nobody argued with them, that\u2019s for sure.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I use Windows PowerShell to retrieve the value of environment variables and the path to a special folder?&#8212; SB Hey, SB. Well, it\u2019s Thursday, which means that the Scripting Guy who writes this column is currently in Walla Walla, WA, which, as Daffy Duck fans all know, is the home [&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":[16,47,3,45],"class_list":["post-64463","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-desktop-management","tag-general-management-tasks","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I use Windows PowerShell to retrieve the value of environment variables and the path to a special folder?&#8212; SB Hey, SB. Well, it\u2019s Thursday, which means that the Scripting Guy who writes this column is currently in Walla Walla, WA, which, as Daffy Duck fans all know, is the home [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64463","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=64463"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64463\/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=64463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64463"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}