Kae Travis

Using Select-Object with PowerShell

A couple of days ago I wanted to check which products were installed on a computer using PowerShell. And throughout my journey I discovered how we can format the output of returned data by using Select-Object with PowerShell.

Take this basic example where we query the ‘Uninstall’ registry key and output the registry data for displayname, displayversion, installdate:

gci -recurse HKLM:Software\Microsoft\Windows\CurrentVersion\Uninstall | get-itemproperty | sort displayname | select-object displayname, displayversion, installdate

But what if we wanted to join/concatenate the displayname and displayversion columns into one column? Or what if we wanted to give the column a custom name? We can do so like his:

gci -recurse HKLM:Software\Microsoft\Windows\CurrentVersion\Uninstall | get-itemproperty | sort displayname | select-object @{Label='Name and Version'; Expression={$_.displayname + " and the version is " + $_.displayversion}}, installdate

So we’ve joined the displayname and displayversion columns into one column called ‘Name and Version’. Great. Now by default the installdate registry data is stored in the yyyyMMdd format, or, as an example 20180807.

This isn’t very readable. So we can also manipulate this format (ok, we don’t do any casting as a date in this example – just basic string manipulation) and call the column ‘Formatted Date’ like so:

gci -recurse HKLM:Software\Microsoft\Windows\CurrentVersion\Uninstall | get-itemproperty | sort displayname | select-object @{Label='Name and Version'; Expression={$_.displayname + " " + $_.displayversion}}, @{Label='Formatted Date'; Expression={$_.installdate.substring(6,2) + "/" + $_.installdate.substring(4,2) + "/" + $_.installdate.substring(2,2)}}

This was quite a crude example, but hopefully it demonstrates how using Select-Object with PowerShell can be used to manipulate column data dynamically.

Using Select-Object with PowerShell
Using Select-Object with PowerShell

Leave a Reply