{"id":3655,"date":"2013-05-11T00:01:00","date_gmt":"2013-05-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/05\/11\/weekend-scripter-playing-with-powershells-get-variable-cmdlet\/"},"modified":"2013-05-11T00:01:00","modified_gmt":"2013-05-11T00:01:00","slug":"weekend-scripter-playing-with-powershells-get-variable-cmdlet","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-playing-with-powershells-get-variable-cmdlet\/","title":{"rendered":"Weekend Scripter: Playing with PowerShell&#039;s Get-Variable Cmdlet"},"content":{"rendered":"<p><strong style=\"font-size: 12px\">Summary<\/strong><span style=\"font-size: 12px\">: Microsoft Scripting Guy, Ed Wilson, spends some time playing around with the <\/span><strong style=\"font-size: 12px\">Get-Variable<\/strong><span style=\"font-size: 12px\"> cmdlet.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning I am doing something a little different. Instead of drinking English Breakfast tea, I woke up feeling like I wanted Oolong tea instead. So I made a pot of Oolong tea with a spoonful of jasmine buds, a half spoon of peppermint leaves, three juniper berries, and half of a crushed cinnamon stick. It is quite invigorating, without being too overbearing.<\/p>\n<h2>Checking the value of a variable<\/h2>\n<p>So I am sitting on the lanai in my porch swing, and I just finished checking the email for the <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a> email alias. I received an email asking why I would bother using the <strong>Get-Variable<\/strong> cmdlet, when I can simply reference the variable to see the value? The reason is that there are many more things I need to do with variables than simply see the value contained inside the variable. The following example assigns a directory listing to the <strong>$dir<\/strong> variable:<\/p>\n<p style=\"padding-left: 30px\">$dir = dir -Directory | ? lastwritetime -gt $([datetime]&#8217;4\/1\/13&#8242;)<\/p>\n<p>To see the value that is contained in the <strong>$dir<\/strong> variable, I can type <strong>$dir<\/strong>. The command and the output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7144.hsg-5-11-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7144.hsg-5-11-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h3>Use Get-Variable<\/h3>\n<p>I can also do this by using the <strong>Get-Variable<\/strong> cmdlet. When use <strong>Get-Variable<\/strong>, both the name and the value appear. To return only the value, I group the expression, and get the <strong>Value<\/strong><em> <\/em>property as shown here:<\/p>\n<p style=\"padding-left: 30px\">(Get-Variable dir).value<\/p>\n<p>This command and the associated output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8308.hsg-5-11-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8308.hsg-5-11-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h3>Use the Variable provider<\/h3>\n<p>But there are other ways to retrieve values from Windows PowerShell variables. For example, I can use the <strong>Variable<\/strong> provider and access the value from the Windows PowerShell Variable drive. I do this by using the command shown here:<\/p>\n<p style=\"padding-left: 30px\">$variable:dir<\/p>\n<p>The command and the output are shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1781.hsg-5-11-13-03.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1781.hsg-5-11-13-03.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Because the <strong>Variable<\/strong> provider creates a PSDrive named <strong>Variable<\/strong>, I can use the standard Windows PowerShell cmdlets to work with the variable. For example, I can use <strong>Get-Item <\/strong>as shown in this example:<\/p>\n<p style=\"padding-left: 30px\">Get-Item -Path Variable:\\dir<\/p>\n<p>But if I only want the value of the <strong>Dir<\/strong> variable, I need to select the <strong>Value<\/strong> property:<\/p>\n<p style=\"padding-left: 30px\">(Get-Item -Path Variable:\\dir).value<\/p>\n<p>The commands and associated output are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2746.hsg-5-11-13-04.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2746.hsg-5-11-13-04.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>One of the cooler things to do is to use the <strong>Get-Content<\/strong> cmdlet to obtain the contents of the variable. When using this technique, only the first value from the array returns. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Content variable:\\dir<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Directory: C:\\<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Mode&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; LastWriteTime&nbsp;&nbsp;&nbsp;&nbsp; Length Name<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#8212;&#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212; &#8212;-<\/p>\n<p style=\"padding-left: 30px\">d&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4\/23\/2013&nbsp; 12:41 PM&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fso<\/p>\n<h2>Examining the type of a variable<\/h2>\n<p>I am sure you know that there are different types of variables in Windows PowerShell. If I use the <strong>Get-Variable<\/strong> cmdlet, pipe the results to the <strong>Format-List<\/strong> cmdlet, and choose all of the properties, I obtain some pretty interesting information. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Variable dir | fl *<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">PSPath&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Microsoft.PowerShell.Core\\Variable::dir<\/p>\n<p style=\"padding-left: 30px\">PSDrive&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Variable<\/p>\n<p style=\"padding-left: 30px\">PSProvider&nbsp;&nbsp;&nbsp; : Microsoft.PowerShell.Core\\Variable<\/p>\n<p style=\"padding-left: 30px\">PSIsContainer : False<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : dir<\/p>\n<p style=\"padding-left: 30px\">Description&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Value&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : {fso, fsox, Program Files, Program Files (x86)&#8230;}<\/p>\n<p style=\"padding-left: 30px\">Visibility&nbsp;&nbsp;&nbsp; : Public<\/p>\n<p style=\"padding-left: 30px\">Module&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">ModuleName&nbsp;&nbsp;&nbsp; :<\/p>\n<p style=\"padding-left: 30px\">Options&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : None<\/p>\n<p style=\"padding-left: 30px\">Attributes&nbsp;&nbsp;&nbsp; : {}<\/p>\n<p>If I take the <strong>$dir<\/strong> variable and pipe it to the <strong>Get-Member<\/strong> cmdlet, it returns information about the objects that are contained in the variable, and not about the variable itself.<\/p>\n<p style=\"padding-left: 30px\">$dir | get-member<\/p>\n<p>The command and output from the command are shown in the image that follows.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3056.hsg-5-11-13-05.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3056.hsg-5-11-13-05.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>To find information about the variable itself, I use <strong>Get-Variable<\/strong> to return the variable, and then I pipe the result to the <strong>Get-Member<\/strong> cmdlet:<\/p>\n<p style=\"padding-left: 30px\">Get-Variable dir | Get-Member<\/p>\n<p>The command and output from the command are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1768.hsg-5-11-13-06.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1768.hsg-5-11-13-06.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<h2>Finding different types of variables<\/h2>\n<p>Unfortunately, this does not return anything about the type of Windows PowerShell variable. So what is the easy way to find the variable type? Use the <strong>Get-Variable<\/strong> cmdlet and pipe the returned objects to the <strong>Get-Member<\/strong> cmdlet. Next, I select only the <strong>TypeName<\/strong> property, and I use the <strong>Unique<\/strong> switch to ensure only unique instances return. Here is the command and the results:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Variable | Get-Member | select typename -Unique<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">TypeName<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">System.Management.Automation.PSVariable<\/p>\n<p style=\"padding-left: 30px\">System.Management.Automation.QuestionMarkVariable<\/p>\n<p style=\"padding-left: 30px\">System.Management.Automation.LocalVariable<\/p>\n<p style=\"padding-left: 30px\">System.Management.Automation.SessionStateCapacityVariable<\/p>\n<p style=\"padding-left: 30px\">System.Management.Automation.NullVariable<\/p>\n<p>When I know the different types of variables, I can use Windows PowerShell to list a particular type of variable. To do this, I pipe the results of <strong>Get-Variable<\/strong> to the <strong>Where-Object<\/strong>. In my <strong>Where-Object<\/strong> filter, I use the <strong>PSObject<\/strong><em> <\/em>property so I can reference the underlying type. This technique is shown here where I find all variables of the type <strong>LocalVariable<\/strong><em>. <\/em><\/p>\n<p style=\"padding-left: 30px\">Get-Variable | where {$_.psobject.typenames -match &#8216;local&#8217;}<\/p>\n<p>The command and output from the command are shown here.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0755.hsg-5-11-13-07.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0755.hsg-5-11-13-07.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Join me tomorrow for the exciting conclusion to the Windows PowerShell and Security series by Yuri Diogenes and Tom Schinder. To catch up, read <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/yuri+diogenes\/\">the two previous posts in this series<\/a>.<\/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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, spends some time playing around with the Get-Variable cmdlet. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am doing something a little different. Instead of drinking English Breakfast tea, I woke up feeling like I wanted Oolong tea instead. So I made a pot of Oolong tea [&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":[3,4,61,45],"class_list":["post-3655","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, spends some time playing around with the Get-Variable cmdlet. Microsoft Scripting Guy, Ed Wilson, is here. This morning I am doing something a little different. Instead of drinking English Breakfast tea, I woke up feeling like I wanted Oolong tea instead. So I made a pot of Oolong tea [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3655","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=3655"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/3655\/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=3655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=3655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=3655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}