PowerShell - Formatting output of Invoke-Command -ScriptBlock

2026-08-12T22:53:25.5966667+00:00

Hi. I'm having difficulty formatting output of Invoke-Command -ScriptBlock. My goal is to loop through a list of servers in a .CSV and run Invoke-Command on each to get selected key value pairs from the registry. The values I'm looking for is the version number of the agent for our cloud-based backup solution. So far I have this:

Invoke-Command -ComputerName $Server -ScriptBlock { Get-ItemPropertyValue -LiteralPath 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall{7DC36D55-4510-4307-9550-57A83DC6A1C2}' -Name "DisplayName", "DisplayVersion" }

Which gives this output for my three sample server entries in the .CSV file:

Rubrik Backup Service

9.4.3

Rubrik Backup Service

9.4.3

Rubrik Backup Service

9.4.3

What I want is a table like:

ServerName AgentName Version


MyServer 1 Rubrik Backup Service 9.4.3

MyServer 2 Rubrik Backup Service 9.4.3

MyServer 3 Rubrik Backup Service 9.4.3

I tried piping the Invoke-Command to Format-Table but it results in errors.

Thanks for your help and sample code!

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

2 answers

Sort by: Newest
  1. Marcin Policht 106.8K Reputation points MVP Volunteer Moderator
    2026-08-13T01:48:18.39+00:00

    Get-ItemPropertyValue returns the two registry values as separate strings, so Invoke-Command outputs them as separate objects. Try having the remote script block construct a single object containing the values you want and include the server name. Then PowerShell can format those objects as a normal table.

    $Servers = Import-Csv "C:\path\servers.csv"
    
    $Results = foreach ($Server in $Servers) {
        Invoke-Command -ComputerName $Server.ServerName -ScriptBlock {
            $RegPath = 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{7DC36D55-4510-4307-9550-57A83DC6A1C2}'
    
            $App = Get-ItemProperty -LiteralPath $RegPath
    
            [PSCustomObject]@{
                ServerName = $env:COMPUTERNAME
                AgentName  = $App.DisplayName
                Version    = $App.DisplayVersion
            }
        }
    }
    
    $Results | Format-Table -AutoSize
    

    That should produce:

    ServerName AgentName Version
    --------------------------------
    MyServer1 Rubrik Backup Service 9.4.3
    MyServer2 Rubrik Backup Service 9.4.3
    MyServer3 Rubrik Backup Service 9.4.3
    

    The remote script block returns one PSCustomObject per server rather than returning DisplayName and DisplayVersion as two independent output values. Also, if your CSV column is not actually named ServerName, replace $Server.ServerName with the actual column name.


    If the above response helps answer your question, remember to "Accept Answer" so that others in the community facing similar issues can easily find the solution. Your contribution is highly appreciated.

    hth

    Marcin

    Was this answer helpful?

    0 comments No comments

  2. AI answer

    2026-08-12T22:53:42.41+00:00

    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-Table at the end of the pipeline.
    • Return objects, not just raw values, when table formatting is needed.
    • For remote commands, PSComputerName is available and can be added to the table output.
    • Format-List and Format-Table can restrict output to selected properties.

    References:

    AI-generated content may be incorrect. Read our transparency notes for more information.

    Was this answer helpful?

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.