Prof. Powershell

Practical PowerShell Part 2: Property Value

This week Prof. PowerShell shows you how to retrieve dynamic property values.

More on this topic:


In last week's lesson we started exploring a PowerShell command and identifying ways to use it. If you want to follow along, open a command prompt and run through the command line examples from the last article to get caught up.

I need to verify that I can retrieve the value from each dynamic property. Here's a quick recap of where we're at:

PS C:\> $regentry = Get-ItemProperty -Path HKLM:\SOFTWARE\RegisteredApplications | Select -Property * -ExcludeProperty $exclude
PS C:\> $props = $regentry.psobject.properties.name

The $props variable contains an array of property names for things in $regentry.

PS C:\> $props[0]
Paint

How about a quick test?

PS C:\> $regentry.$($props[0])
SOFTWARE\Microsoft\Windows\CurrentVersion\Applets\Paint\Capabilities

Excellent. Remember, my goal is to also get some information from the corresponding value. I am assuming this path is relative to HKLM so I will use Join-Path to construct a valid path which I can use with Get-Item.

PS C:\> Join-Path HKLM: $regentry.$($props[0]) | Get-Item

[Click on image for larger view.]  Figure 1.

Or if I just want the properties:

PS C:\> Join-Path HKLM: $regentry.$($props[0]) | Get-ItemProperty | Select App*

[Click on image for larger view.]  Figure 2.

Now that I've proved how to do something for one item in my list of registered applications I can do it for all using ForEach-Object.

foreach ($entry in $props) {
$valuepath = Join-Path -path "HKLM:" -child $regentry.$entry
Get-ItemProperty -Path $valuePath | Select App*
}

[Click on image for larger view.]  Figure 3.

As you can see in Figure 3, it is possible some applications may not have anything at the specified location. I will have to handle that error when it comes time to build a reusable script. I always prefer writing objects to the pipeline. I could take what I have discovered so far and use Select-Object to create a custom object for each registered application.

$props | Select @{Name="Application";Expression={$_}},
@{Name="Entry";Expression={$regentry.$_}},
@{Name="Name";Expression={
(Join-Path -path "HKLM:" -child $regentry.$_ | Get-ItemProperty).ApplicationName}},
@{Name="Description";Expression={
(Join-Path -path "HKLM:" -child $regentry.$_ | Get-ItemProperty).ApplicationDescription}}

[Click on image for larger view.]  Figure 4.

As you can see in Figure 4 this works, although perhaps it isn't that easy to follow. The $_ place holder is the name of each registered application which is used as a dynamic property of the previously created $regentry variable. Of course I could add and delete properties at will. Although personally, I have a goal to write or use a command only once and in this instance I am creating the same path and getting the same registry property twice. Yes, the performance hit is negligible, but I prefer something a bit more efficient. That is what we will look at next time.


 

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular

Upcoming Training Events