how to get output from Receive-Job in variable – powershell trick

how to get output from Receive-Job in to variable?

Based on my research is it not possible to assign output of Receive-Job to variable without writing on host, but there is any alternative which can allow us to pass all error, warning, success msg to output stream and write on host as well.

how can we do that ?

Below is command to get the $job  and receive the job output in $status1 . here we have used *>&1 which means pass all error, warning, information to output stream.

$status1=get-job -name $job | Receive-Job  *>&1

But the problem with above command is , it also print the output to host

you can use $status1 to see the outcome of the job


Here is full and tested script, you can modify as per your requirement


$process = Get-Process |select -First 2 |select name
# adding one more process name to validate if the error getting captured in output stream
foreach($process1 in $process)
$processname = $process1
$jobids+=Start-Job -ScriptBlock{ 
Get-Process $($
write-host "$env:COMPUTERNAME and $ar"} -ArgumentList $processname

# Checking if all jobs are completed
foreach($jobid in $jobids)
write-host "waiting"
while((get-job -Name $($ -eq "running")

# get job output
foreach($jobid in $jobids)
if((get-job -Name $($ -eq "completed")
$job = get-job -Name $($
#if you dont want to print anything on host then use invoke or else you can directly use receive-job with *>&1
$status1 = Invoke-Command -ScriptBlock { Receive-Job -Job $job *>&1 }
write-host "output for $($ is - $status1"
write-host "$($ is $((get-job -Name $($"

(Visited 1,083 times, 2 visits today)

Add a Comment