{"id":8491,"date":"2014-12-24T06:00:00","date_gmt":"2014-12-24T14:00:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=8491"},"modified":"2016-04-08T16:38:45","modified_gmt":"2016-04-08T23:38:45","slug":"find-variables-with-class","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2014\/12\/24\/find-variables-with-class\/","title":{"rendered":"Find variables with class"},"content":{"rendered":"<p><a class=\"twitter-follow-button\" href=\"https:\/\/twitter.com\/juneb_get_help\" data-lang=\"en\">Follow @juneb_get_help<\/a><\/p>\n<p>I&#8217;ve been playing with the Windows PowerShell 5.0 preview and especially with its new <a href=\"http:\/\/www.sapien.com\/blog\/2014\/12\/02\/beyond-custom-objects-create-a-net-class\/\" target=\"_new\">class<\/a> feature. After I create my class, I often create several <em>instances<\/em> of the class and experiment with them in my session. For example, I created a Wine class and then created several instances of my wine class. I saved each Wine instance in a variable.<\/p>\n<pre lang=\"PowerShell\">$PSWine = [Wine]::new(\"PSCabernet\")\r\n$Shiraz = [Wine]::new()\r\n...<\/pre>\n<p>After playing with the instances for a while (and enjoying a few sips of PSWine), I couldn&#8217;t remember all of the Wine objects that I&#8217;d created in my session. They were all in variables, so I used the Get-Variable cmdlet to find the $PSWine variable.<\/p>\n<pre class=\"output\">PS C:\\&gt; Get-Variable \u2013Name PSWine\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ------\r\nPSWine\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Wine<\/pre>\n<p>Excellent! The value of the variable is the name of the class. This should make it easy to find the other variables.<\/p>\n<pre class=\"output\">PS C:\\&gt; Get-Variable | Where Value \u2013eq \"Wine\"\r\n\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -----\r\n?\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 True\r\ntrue\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 True<\/pre>\n<p>That didn&#8217;t work, because the value of the Value property appears to contain &#8220;Wine&#8221;, but that&#8217;s just a display artifact. The Value of the variable is the entire Wine object.<\/p>\n<pre class=\"output\">PS C:\\&gt; (Get-Variable -Name PSWine).Value\r\n\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : PSCabernet\r\nWinery\u00a0\u00a0\u00a0\u00a0\u00a0 : Chateau Snover\r\nYear\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : 2012\r\nisSparkling : False\r\nColor\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : Red\r\nSweetness\u00a0\u00a0 : VeryDry\r\nDescription : Pithy\r\nRating\u00a0\u00a0\u00a0\u00a0\u00a0 : {96, 94}<\/pre>\n<p>To get Wine objects in the session, or objects of any particular type, such as strings or integers, call the <strong>GetType()<\/strong> method of the value and then take its <strong>Name<\/strong> property.<\/p>\n<pre class=\"output\">PS C:\\&gt; Get-Variable | where\u00a0 {$_.value.gettype().Name -eq \"Wine\"}\r\n\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -----\r\nDuck\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Wine\r\n\r\nYou cannot call a method on a null-valued expression.\r\nAt line:1 char:24\r\n+ Get-Variable | where\u00a0 {$_.value.gettype().Name -eq \"Wine\"}\r\n+\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\r\n+ CategoryInfo\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 : InvalidOperation: (:) [], RuntimeException\r\n+ FullyQualifiedErrorId : InvokeMethodOnNull\r\n\r\nPSWine\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Wine\r\nShiraz\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Wine<\/pre>\n<p>Oops! You have to account for variables that don&#8217;t have a value. I&#8217;ll use the <strong>AND<\/strong> operator to make sure that the variable has a value before I try to get the name of its value type. Windows PowerShell has a &#8220;lazy&#8221; <strong>AND<\/strong> operator, which means that if the first part of the <strong>AND<\/strong> statement isn&#8217;t true, since both parts must be true, it doesn&#8217;t even check the second part.<\/p>\n<pre class=\"output\">PS C:\\ps-test&gt; Get-Variable | where {$_.Value -and $_.Value.GetType().Name -eq \"Wine\"}\r\n\r\nName\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Value\r\n----\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 -----\r\nDuck\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Wine\r\nPSWine\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Wine\r\nShiraz\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Wine<\/pre>\n<p>It works on all types.<\/p>\n<pre class=\"output\">PS C:\\ps-test&gt; Get-Variable | \r\nwhere {$_.Value -and $_.Value.GetType().Name -eq \"ActionPreference\"}\r\n\r\nName                           Value\r\n----                           -----\r\nErrorActionPreference          Continue\r\nProgressPreference             Continue\r\nWarningPreference              Continue<\/pre>\n<p>Let&#8217;s turn this into a function:<\/p>\n<pre lang=\"PowerShell\">function Get-VariableOfType ($Type)\r\n{\r\n    Get-Variable | where {$_.Value -and $_.Value.GetType().Name -eq\u00a0 $type}\r\n}<\/pre>\n<p>Now, back to that Wine glass &#8212;\u00a0 I mean class!<\/p>\n<p><em>June Blender is a technology evangelist at SAPIEN Technologies, Inc. You can reach her at <\/em><a href=\"mailto:juneb@sapien.com\"><em>juneb@sapien.com<\/em><\/a><em> or follow her on Twitter at <\/em><a href=\"https:\/\/twitter.com\/juneb_get_help\"><em>@juneb_get_help<\/em><\/a><em>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Follow @juneb_get_help I&#8217;ve been playing with the Windows PowerShell 5.0 preview and especially with its new class feature. After I create my class, I often create several instances of the class and experiment with them in my session. For example, I created a Wine class and then created several instances of my wine class. I [&hellip;]<\/p>\n","protected":false},"author":31,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1033,941,25],"tags":[934,28],"class_list":["post-8491","post","type-post","status-publish","format-standard","hentry","category-classes-in-powershell-5-0","category-powershell-5-0","category-windows-powershell","tag-juneb","tag-powershell"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/8491","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/31"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=8491"}],"version-history":[{"count":12,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/8491\/revisions"}],"predecessor-version":[{"id":8504,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/8491\/revisions\/8504"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=8491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=8491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=8491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}