Parsing an Object

All,
I have a block of code I pulled from the interwebs and modified for my needs that is providing exactly what I expect. This outputs data (Properties/Values) that I would like to work with. However, I can’t seem to find a way to parse the output.

The code is this;

$Regpath1 = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Updates\Microsoft .NET Framework 4.7.2\KB4087364"

Push-Location 
Set-Location $Regpath1

$a = Get-Item $Regpath1 | Select-Object -ExpandProperty property |

     ForEach-Object {

           New-Object psobject -Property @{
            “Property” =  $_;
            “Value”    =  (Get-ItemProperty -Path . -Name $_).$_}} |

            Format-Table Property, value -AutoSize
            Pop-Location
$a

The output is this;

Because I have had such trouble with this, I ran $a.GetType() to figure out what I am working with. That says this is an Object with a BaseType of System.Array. That seems to indicate that this is an Object and an Array. But I can’t seem to filter or pull properties or values out of this output in any way.
What am I missing?

What’s the question?

Your problem is that the loop contains a Format-* cmdlet so $a references formatted data, not the object(s) being created in the loop. Remove the | and Format-Table cmdlet and then you can access $a.propertyName

Edit: Well that’ll teach me to test before posting…
It requires a bit more modification to access by property name.

$Regpath1 = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Updates\Microsoft .NET Framework 4.7.2\KB4087364"

Push-Location 
Set-Location $Regpath1

$a = Get-Item $Regpath1 | Select-Object -ExpandProperty property |

     ForEach-Object {

        [PSCustomObject] @{
            $_ =  (Get-ItemProperty -Path . -Name $_).$_
        }
    } 

     Pop-Location

$a

Thanks @matt-bloomfield. I like how this produces the same output as my original, but with fewer lines. I will use that for learning. However, I am seeing the same issue with my attempts to pull data from that output. For example, trying to grab the Name and Value for InstalledDate so it could be used downstream.
$a. just complains that the property does not exist.

I would change it to PSCustomObject

$Regpath1 = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Updates\Microsoft .NET Framework 4.7.2\KB4087364"

Push-Location $Regpath1

$a = Get-Item $Regpath1 | Select-Object -ExpandProperty property |

     ForEach-Object {

        [PSCustomObject] @{
            $_ =  (Get-ItemProperty -Path . -Name $_).$_
        }
    }

Pop-Location

$a

Then you can access by

$a.installeddate

assuming that property was present in the registry path.

@krzydoug perhaps that is the issue. If it is, I don’t understand. The properties that list in $a are within the key ‘KB4087364’. This is how the variable gets populated so I can read it in the console. But any attempt to use ‘$a.anything’ is met with an error about no property.
This is really narrowing down the core issue. I can get the data I want written to a variable, but not work with any of that data.

Thanks, I’ve corrected it.

1 Like

Please give us the output of

$a | Get-Member

Sure, here you go.

That’s not the result of either code provided by me or Matt.

That’s what I’m runnin’, and that’s the result. I wonder if an ISE glitch? I will restart it.

Nope, same. If I run it in the console, same

Could you please stop posting pictures of code or console output? That’s not helpful. Just format it as code just like you did it in your initial post.

Thanks in advance.

1 Like

Change to PSCustomObject

$Regpath1 = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Updates\Microsoft .NET Framework 4.7.2\KB4087364"

Push-Location $Regpath1

$a = Get-Item $Regpath1 | Select-Object -ExpandProperty property |

     ForEach-Object {

        [PSCustomObject] @{
            $_ =  (Get-ItemProperty -Path . -Name $_).$_
        }
    }

Pop-Location

$a

Done. It provided almost no output.

$Regpath1 = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Updates\Microsoft .NET Framework 4.7.2\KB4087364"

Push-Location $Regpath1

$a = Get-Item $Regpath1 | Select-Object -ExpandProperty property |

     ForEach-Object {

        [PSCustomObject] @{
            $_ =  (Get-ItemProperty -Path . -Name $_).$_
        }
    }

Pop-Location

$a

ThisVersionInstalled
--------------------
Y    

Well you have to blame Matt for that. :rofl:

LOL. I will test PSCustomObject with my original code in that case.

Here is what I get.


PS J:\> $a | Get-Member


   TypeName: System.Management.Automation.PSCustomObject

Name        MemberType   Definition                                 
----        ----------   ----------                                 
Equals      Method       bool Equals(System.Object obj)             
GetHashCode Method       int GetHashCode()                          
GetType     Method       type GetType()                             
ToString    Method       string ToString()                          
Property    NoteProperty System.String Property=ThisVersionInstalled
Value       NoteProperty string Value=Y   

Here give this a try.

$Regpath1 = "HKLM:\SOFTWARE\WOW6432Node\Microsoft\Updates\Microsoft .NET Framework 4.7.2\KB4087364"

Push-Location $Regpath1

$a = Get-Item $Regpath1 |
     ForEach-Object {
        $ht = [ordered]@{}
        foreach($prop in $_.property){
            $ht.add($prop,(Get-ItemProperty -Path $_.pspath).$prop)
        }
        [PSCustomObject]$ht
     }

Pop-Location

$a
2 Likes

Outstanding! If I understand where we were trying to get, this does it. I don’t fully understand it yet, but it looks good.

PS J:\> $a | Get-Member


   TypeName: System.Management.Automation.PSCustomObject

Name                 MemberType   Definition                                                                                                                            
----                 ----------   ----------                                                                                                                            
Equals               Method       bool Equals(System.Object obj)                                                                                                        
GetHashCode          Method       int GetHashCode()                                                                                                                     
GetType              Method       type GetType()                                                                                                                        
ToString             Method       string ToString()                                                                                                                     
ARPLink              NoteProperty string ARPLink=HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{92FB6C44-E685-45AD-9B20-CADF4CABA132}.KB4087364
InstalledBy          NoteProperty string InstalledBy=SYSTEM                                                                                                             
InstalledDate        NoteProperty string InstalledDate=7/25/2020                                                                                                        
InstallerName        NoteProperty string InstallerName=Windows Installer                                                                                                
InstallerVersion     NoteProperty string InstallerVersion=5.00                                                                                                          
PackageName          NoteProperty string PackageName=Update for Microsoft .NET Framework 4.7.2 (KB4087364)                                                              
PackageVersion       NoteProperty string PackageVersion=1                                                                                                               
Publisher            NoteProperty string Publisher=Microsoft Corporation                                                                                                
PublishingGroup      NoteProperty string PublishingGroup=Developer Division                                                                                             
ReleaseType          NoteProperty string ReleaseType=Update                                                                                                             
ThisVersionInstalled NoteProperty string ThisVersionInstalled=Y                                                                                                         

Thank you guys. This gets me a step closer.

1 Like

You’re welcome fellow human.

1 Like