Get-ItemPropertyValue returns only the values, so Invoke-Command emits separate strings instead of an object with named properties. For table output, return a single object from the remote script block, then format it after the pipeline.
Use Get-ItemProperty and build a custom object:
$servers = Import-Csv .\servers.csv
foreach ($row in $servers) {
Invoke-Command -ComputerName $row.ServerName -ScriptBlock {
$app = Get-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{7DC36D55-4510-4307-9550-57A83DC6A1C2}'
[pscustomobject]@{
ServerName = $env:COMPUTERNAME
AgentName = $app.DisplayName
Version = $app.DisplayVersion
}
}
} | Format-Table -AutoSize
If running against multiple computers in one call, Invoke-Command also returns the PSComputerName property. That can be displayed directly:
$servers = (Import-Csv .\servers.csv).ServerName
Invoke-Command -ComputerName $servers -ScriptBlock {
$app = Get-ItemProperty -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{7DC36D55-4510-4307-9550-57A83DC6A1C2}'
[pscustomobject]@{
AgentName = $app.DisplayName
Version = $app.DisplayVersion
}
} | Format-Table PSComputerName, AgentName, Version -AutoSize
Key points:
- Put
Format-Tableat the end of the pipeline. - Return objects, not just raw values, when table formatting is needed.
- For remote commands,
PSComputerNameis available and can be added to the table output. -
Format-ListandFormat-Tablecan restrict output to selected properties.
References: