User Rating: 0 / 5

Star InactiveStar InactiveStar InactiveStar InactiveStar Inactive
 

I’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 saved each Wine instance in a variable.

$PSWine = [Wine]::new("PSCabernet")
$Shiraz = [Wine]::new()
...

After playing with the instances for a while (and enjoying a few sips of PSWine), I couldn’t remember all of the Wine objects that I’d created in my session. They were all in variables, so I used the Get-Variable cmdlet to find the $PSWine variable.

PS C:\> Get-Variable –Name PSWine
Name               Value
----               ------
PSWine             Wine

Excellent! The value of the variable is the name of the class. This should make it easy to find the other variables.

PS C:\> Get-Variable | Where Value –eq "Wine"

Name                           Value
----                           -----
?                              True
true                           True

That didn’t work, because the value of the Value property appears to contain “Wine”, but that’s just a display artifact. The Value of the variable is the entire Wine object.

PS C:\> (Get-Variable -Name PSWine).Value

Name        : PSCabernet
Winery      : Chateau Snover
Year        : 2012
isSparkling : False
Color       : Red
Sweetness   : VeryDry
Description : Pithy
Rating      : {96, 94}

To get Wine objects in the session, or objects of any particular type, such as strings or integers, call the GetType() method of the value and then take its Name property.

PS C:\> Get-Variable | where  {$_.value.gettype().Name -eq "Wine"}

Name                           Value
----                           -----
Duck                           Wine

You cannot call a method on a null-valued expression.
At line:1 char:24
+ Get-Variable | where  {$_.value.gettype().Name -eq "Wine"}
+                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

PSWine                         Wine
Shiraz                         Wine

Oops! You have to account for variables that don’t have a value. I’ll use the AND 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 “lazy” AND operator, which means that if the first part of the AND statement isn’t true, since both parts must be true, it doesn’t even check the second part.

PS C:\ps-test> Get-Variable | where {$_.Value -and $_.Value.GetType().Name -eq "Wine"}

Name                           Value
----                           -----
Duck                           Wine
PSWine                         Wine
Shiraz                         Wine

It works on all types.

PS C:\ps-test> Get-Variable | 
where {$_.Value -and $_.Value.GetType().Name -eq "ActionPreference"}

Name                           Value
----                           -----
ErrorActionPreference          Continue
ProgressPreference             Continue
WarningPreference              Continue

Let’s turn this into a function:

function Get-VariableOfType ($Type)
{
    Get-Variable | where {$_.Value -and $_.Value.GetType().Name -eq  $type}
}

Now, back to that Wine glass —  I mean class!

June Blender is a technology evangelist at SAPIEN Technologies, Inc. You can reach her at This email address is being protected from spambots. You need JavaScript enabled to view it. or follow her on Twitter at @juneb_get_help.

If you have questions about our products, please post in our support forum.
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Trial Software Questions forum.
Copyright © 2017 SAPIEN Technologies, Inc.