{"id":71902,"date":"2015-07-27T00:01:00","date_gmt":"2015-07-27T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/07\/27\/describe-your-powershell-variables\/"},"modified":"2019-02-18T09:46:50","modified_gmt":"2019-02-18T16:46:50","slug":"describe-your-powershell-variables","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/describe-your-powershell-variables\/","title":{"rendered":"Describe Your PowerShell Variables"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about adding a description to variables.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. There is a &ldquo;water feature&rdquo; outside that keeps running and running and running. It is a bit strange, and to be honest, it makes me think that it is raining all the time. In central Florida in the summer, it is generally a safe bet that it will rain, but even when the sun is outside, without a cloud in the sky, the giant frog keeps spitting water into a pond. I don&rsquo;t get it. It is not relaxing like a Zen pond, and it does not keep bugs at bay like a real frog would do. It just sits there.<\/p>\n<p>Sometimes I run across things on my computer that are bit like that ceramic frog, and I just don&rsquo;t get it. I see a user account and wonder, &ldquo;Why was that created?&rdquo; Or I see a group that causes the same question.<\/p>\n<p>On the network, it is even worse, because there are multiple admins, and often a bit of turnover, so no one knows why something was created. Unfortunately, the old technique of &ldquo;delete it and see who complains&rdquo; does not work for today&rsquo;s fast-paced environments. I mean, who in their right mind wants to create additional work for themselves.<\/p>\n<p>And unfortunately, &ldquo;see who complains&rdquo; might not complain for several months&mdash;in which time you forget what you deleted in the first place. So it means hours of troubleshooting (because if I really knew the app in the first place, I would know what the &quot;thing&quot; I was getting ready to delete would do).<\/p>\n<p>Luckily, with Windows PowerShell, I can fix some of that.<\/p>\n<h2>Add descriptions to global variables<\/h2>\n<p>In my profile last week, I added two variables that I want to always be available to me. These are global variables (meaning I can use them in functions, modules, and the like), and I really don&rsquo;t want them changed. In fact, the reason I created them was so that I would have a consistent environment.<\/p>\n<p>But unfortunately, when I created them, I did not ensure any of that. To make matters worse, I did not even document why I was creating the variables in the first place.<\/p>\n<p>Luckily, I can easily fix that by adding various parameters of from the <b>New-Variable<\/b> cmdlet. I am often asked why we have a <b>New-Variable<\/b> cmdlet when I can create a variable by simply making an assignment to the variable, for example:<\/p>\n<p style=\"margin-left:30px\">$a = 1<\/p>\n<p>And why do we have the <b>New-Variable<\/b> cmdlet when I can create a new variable by adding it to the variable drive like this:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; New-Item -Name b -Value 2 -Path variable:<\/p>\n<p style=\"margin-left:30px\">Name&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; Value<\/p>\n<p style=\"margin-left:30px\">&#8212;-&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; &#8212;&#8211;<\/p>\n<p style=\"margin-left:30px\">b&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; 2<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $b<\/p>\n<p style=\"margin-left:30px\">2<\/p>\n<p>Both of these techniques work fine if I am not concerned about adding things like a description, or scope, or whether it is Read-only. I can assign the additional properties to a variable by using the <b>Set-Variable<\/b> cmdlet. As shown here, if I do not set these additional properties, the only the name and value are assigned:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; $a = 1<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-Variable a<\/p>\n<p style=\"margin-left:30px\">Name&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; Value<\/p>\n<p style=\"margin-left:30px\">&#8212;-&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; &#8212;&#8211;<\/p>\n<p style=\"margin-left:30px\">a&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; 1<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-Variable a | fl *<\/p>\n<p style=\"margin-left:30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : a<\/p>\n<p style=\"margin-left:30px\">Description :<\/p>\n<p style=\"margin-left:30px\">Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : 1<\/p>\n<p style=\"margin-left:30px\">Visibility&nbsp; : Public<\/p>\n<p style=\"margin-left:30px\">Module&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"margin-left:30px\">ModuleName&nbsp; :<\/p>\n<p style=\"margin-left:30px\">Options&nbsp;&nbsp;&nbsp;&nbsp; : None<\/p>\n<p style=\"margin-left:30px\">Attributes&nbsp; : {}<\/p>\n<p>When I use <b>Set-Variable<\/b> to add a description, I can pick it up with the <b>Get-Variable<\/b> cmdlet:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Set-Variable a -Description &quot;my variable&quot;<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; Get-Variable a | select name, description<\/p>\n<p style=\"margin-left:30px\">Name&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;&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; Description<\/p>\n<p style=\"margin-left:30px\">&#8212;-&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;&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; &#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"margin-left:30px\">a&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;&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; my variable<\/p>\n<p><b>Note&nbsp;<\/b> When using <b>Get-Variable<\/b> or <b>Set-Variable<\/b>, I do not use the dollar sign ( <b>$<\/b> ) in my variable name.<\/p>\n<h2>Include the description and options<\/h2>\n<p>As a best practice, I should include the description and options for my variables when I create them in my profile. In this way, I can easily find them. In fact, if I use the same description or the same words in my description, I will be able to find my specific profile-created variables. Here are the changes I make to my two variables in my profile:<\/p>\n<p style=\"margin-left:30px\">#Variables<\/p>\n<p style=\"margin-left:30px\">New-Variable -Name doc -Value &quot;$home\\documents&quot; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; -Description &quot;My documents library. Profile created&quot; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; -Option ReadOnly -Scope &quot;Global&quot;<\/p>\n<p style=\"margin-left:30px\">if(!(Test-Path variable:backupHome))<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;new-variable -name backupHome -value &quot;$doc\\WindowsPowerShell\\profileBackup&quot; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; -Description &quot;Folder for profile backups. Profile created&quot; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp; -Option ReadOnly -Scope &quot;Global&quot;<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>When I close my Windows PowerShell console, and open it up again, the changes take effect. I can now find my profile-created variables easily. This is shown here:<\/p>\n<p style=\"margin-left:30px\">PS C:\\&gt; (Get-Variable).where({$_.description -match &#039;profile created&#039;})<\/p>\n<p style=\"margin-left:30px\">Name&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; Value<\/p>\n<p style=\"margin-left:30px\">&#8212;-&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; &#8212;&#8211;<\/p>\n<p style=\"margin-left:30px\">backupHome&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; C:\\Users\\ed\\documents\\WindowsPowerShell\\profileBackup<\/p>\n<p style=\"margin-left:30px\">doc&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; C:\\Users\\ed\\documents<\/p>\n<p>I can now pipe the output to the <b>Format-List<\/b> cmdlet and see the additional information. This is shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Hsg-7-27-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Hsg-7-27-15-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to using a variable description. Join me tomorrow when I will talk more about Windows PowerShell profiles.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about adding a description to variables. Microsoft Scripting Guy, Ed Wilson, is here. There is a &ldquo;water feature&rdquo; outside that keeps running and running and running. It is a bit strange, and to be honest, it makes me think that it is raining all the time. In central [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,144,3,4,605,45],"class_list":["post-71902","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-profiles","tag-scripting-guy","tag-scripting-techniques","tag-variables","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about adding a description to variables. Microsoft Scripting Guy, Ed Wilson, is here. There is a &ldquo;water feature&rdquo; outside that keeps running and running and running. It is a bit strange, and to be honest, it makes me think that it is raining all the time. In central [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71902","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=71902"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71902\/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=71902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}