user3342256
user3342256

Reputation: 1290

Concatenating Two Properties from a Select-Object Statement

I am selecting the DisplayName and DisplayVersion properties of an installed application like so:

$response = Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |  Where-Object {$_.DisplayName -like '*My Application*'} | Select-Object DisplayName, DisplayVersion | ft -HideTableHeaders

The result is: My Application 1.2

For additional parsing purposes I need to concatenate the result with a pipe character so it returns as: My Application|1.2

However I'm unable to find the right syntax.

Upvotes: 3

Views: 1188

Answers (2)

Stefan Prugg
Stefan Prugg

Reputation: 314

try something like this

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |  Where-Object {$_.DisplayName -like '*office*'} | Select-Object @{Expression={$_.DisplayName + "|" + $_.DisplayVersion}} | ft -HideTableHeaders

Using -join:

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |  Where-Object {$_.DisplayName -like '*office*'} | Select-Object @{Expression={$_.DisplayName, $_.DisplayVersion -join "|"}} | ft -HideTableHeaders

Excursus with ConvertTo-CSV

You could use ConvertTo-CSV, but if you require having no header, you need to use -skip 1, which may not always be comprehensible. Additionally, you may have to remove quotes from the output, which is an additional effort.

Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*| Where-Object {$_.DisplayName -like '*office*'} | Select-Object DisplayName, DisplayVersion | ConvertTo-Csv  -Delimiter '|' -NoTypeInformation | select -skip 1

Upvotes: 1

user6811411
user6811411

Reputation:

Use a calculated property to combine the two single properties:

$response = (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\* |  
  Where-Object {$_.DisplayName -like '*My Application*'} | 
    Select-Object  @{n='DisplayNameVersion';e={$_.DisplayName,$_.DisplayVersion -join '|'}}).DisplayNameVersion

Upvotes: 5

Related Questions