Possible to change a property’s object type for each item in a collection?

(My title isn’t very good – I was able to change the [string]’s to [datetime]’s but I want to also keep the same PSCustomObject (or one with the same amount of properties) I started with)

I have a collection of registry keys from:

Get-ItemProperty -path 'HKLM:SOFTWAREMicrosoftWindowsCurrentVersionUninstall*' 

This returns PSCustomObjects with many properties, one of which is [string]InstallDate. I need to change all of them to [datetime] to run a comparison against another [datetime] the user submits. I want to keep the structure of the PSCustomObject’s intact so I can access other properties at the end like DisplayName, InstallLocation, etc.

Is there some way to change all the [string]InstallDate properties to [datetime]’s while preserving the other properties of these registry objects?

I got to this point:

Get-ItemProperty -Path $paths | Where-Object {$_.DisplayName -like "*$name*"} | ForEach-Object {$_.InstallDate.Insert(4,'/').Insert(7,'/')} | ForEach-Object {[datetime]::ParseExact($_, "yyyy/MM/dd", $null) 

but then I am only left with the dates and no other properties. At this point I was going to create a custom object and build each property manually, but I have never worked with custom objects and wanted to see if there is a simple way to just change property object types for each instance.

Thank you

submitted by /u/salamancas
[link] [comments]

Leave a Reply