Using Group-Object to Separate Remoting Results

by Jan 9, 2018

When you need to get information from multiple computers via PowerShell remoting, you could query each machine separately. A much faster way is to query all machines at the same time, which raises the question how you can later separate the results.

Here is a sample (assuming PowerShell remoting is enabled in your environment):

$serverList = 'TRAIN11','TRAIN12'

$code = {

# get results from all machines in parallel...
$results = Invoke-Command -ScriptBlock $code -ComputerName $serverList |
   # and separate results by PSComputerName
   Group-Object -Property PSComputerName -AsHashTable -AsString 



As you see, Invoke-Command returns the requested service information from both machines. Group-Object then separates the data into groups and uses the PSComputerName property for this. PSComputerName is an automatic property that is always available when you receive objects via PowerShell remoting.

As a result, even though PowerShell remoting has executed the code on all machines in parallel, you get separate results per machine.

Twitter This Tip! ReTweet this Tip!