# Saving the output of Receive-Job to variable

**URL:** <https://forums.powershell.org/t/saving-the-output-of-receive-job-to-variable/329>\
**Category:** PowerShell Help\
**Created:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/saving-the-output-of-receive-job-to-variable/329 "2011-12-31T18:30:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![archives](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@archives](https://forums.powershell.org/u/archives)\
**Post date:** [December 31, 2011, 6:30pm UTC](https://forums.powershell.org/t/saving-the-output-of-receive-job-to-variable/329/1 "2011-12-31T18:30:00Z")

</div>

by the\_hutch at 2012-11-13 13:43:39

> I have 4 background jobs running at one time. I can view the output easily enough with: ` Get-Job | Receive-Job ` I would like to assign this output to a variable. Is there any way to do this?   
>   
> I tried this (didn’t work):  
> `$Output = Get-Job | Receive Job`  
>   
> Thanks in advance.  
>   
> The whole code (with IP addresses obscured with ##) can be seen below:  
>   
> `cd C:\pinger<br>$ErrorActionPreference = "silentlycontinue"##Runs Pinger to collect IP addresses from each of the 4 Class B rangesStart-Sleep 5$job1 = { .\pinger.exe ###.###.0.0-###.###.255.255 -h Range1.txt }write-host "Running ###.### subnet"Start-Job -ScriptBlock $job1While (Get-Job -State "Running") {Start-Sleep 30}$job2 = { .\pinger.exe ###.###.0.0-###.###.255.255 -h Range2.txt }write-host "Running ###.### subnet"Start-Job -ScriptBlock $job2While (Get-Job -State "Running") {Start-Sleep 30}$job3 = { .\pinger.exe ###.###.0.0-###.###.255.255 -h Range3.txt }write-host "Running ###.### subnet"Start-Job -ScriptBlock $job3While (Get-Job -State "Running") {Start-Sleep 30}$job4 = { .\pinger.exe ###.###.0.0-###.###.255.255 -h Range4.txt }write-host "Running ###.### subnet"Start-Job -ScriptBlock $job4While (Get-Job -State "Running") {Start-Sleep 30}Get-Job | Stop-JobGet-Job | Remove-Job$Range1 = Get-Content .\Range1.txt$Range2 = Get-Content .\Range2.txt$Range3 = Get-Content .\Range3.txt$Range4 = Get-Content .\Range4.txt$job1 = { param($Range1) ($Range1) | ForEach-Object -Process { $IP = "$" ##Run Commands to Pull Info From and assign output to String $NSLookup = nslookup $IP $strNS = "$NSLookup" $NBTstat = nbtstat -A $IP $strNBT = "$NBTstat" ##Get FQDN from nslookup IF($strNS -like ‘Name’) { $A1 = $strNS.Split(":") $S1 = $A1[3] $A2 = $S1.Split(" ") $FQDN = $A2[4] } ELSE { $FQDN = "FQDN NOT FOUND" } ##Get Hostname from FQDN IF($strNS -like ‘Name’) { $A1 = $FQDN.Split(".") $NSHost = $A1[0] } ELSE { $NSHostName = "NS HOST NOT FOUND" } ##Get MAC and Host from nbtstat IF($strNBT -like ‘Host not found’) { $MAC = "MAC NOT FOUND" $NBTHost = "NBT HOST NOT FOUND" } ELSE { $A1 = $strNBT.Split("=") $S1 = $A1[1] $A2 = $S1.Split(" ") $PreMAC = $A2[1] $MAC = $PreMAC.Replace("-",":") $A3 = $strNBT.Split(" ") $S2 = $A3[73] $A4 = $S2.Split("<") $NBTHost = $A4[0] } $all="$IPt$NSHostt$NBTHostt$MACt$FQDN" write-host "$all" }}Start-Sleep 5Start-Job -ScriptBlock $job1 -ArgumentList (,$Range1)$job2 = { param($Range2) ($Range2) | ForEach-Object -Process { $IP = "$" ##Run Commands to Pull Info From and assign output to String $NSLookup = nslookup $IP $strNS = "$NSLookup" $NBTstat = nbtstat -A $IP $strNBT = "$NBTstat" ##Get FQDN from nslookup IF($strNS -like ‘Name’) { $A1 = $strNS.Split(":") $S1 = $A1[3] $A2 = $S1.Split(" ") $FQDN = $A2[4] } ELSE { $FQDN = "FQDN NOT FOUND" } ##Get Hostname from FQDN IF($strNS -like ‘Name’) { $A1 = $FQDN.Split(".") $HostName = $A1[0] } ELSE { $HostName = "HostName NOT FOUND" } ##Get MAC and Host from nbtstat IF($strNBT -like ‘Host not found’) { $MAC = "MAC NOT FOUND" $NBTHost = "NBT HOST NOT FOUND" } ELSE { $A1 = $strNBT.Split("=") $S1 = $A1[1] $A2 = $S1.Split(" ") $PreMAC = $A2[1] $MAC = $PreMAC.Replace("-",":") $A3 = $strNBT.Split(" ") $S2 = $A3[73] $A4 = $S2.Split("<") $NBTHost = $A4[0] } $all="$IPt$NSHostt$NBTHostt$MACt$FQDN" write-host "$all" }}Start-Sleep 5Start-Job -ScriptBlock $job2 -ArgumentList (,$Range2)$job3 = { param($Range3) ($Range3) | ForEach-Object -Process { $IP = "$" ##Run Commands to Pull Info From and assign output to String $NSLookup = nslookup $IP $strNS = "$NSLookup" $NBTstat = nbtstat -A $IP $strNBT = "$NBTstat" ##Get FQDN from nslookup IF($strNS -like ‘Name’) { $A1 = $strNS.Split(":") $S1 = $A1[3] $A2 = $S1.Split(" ") $FQDN = $A2[4] } ELSE { $FQDN = "FQDN NOT FOUND" } ##Get Hostname from FQDN IF($strNS -like ‘Name’) { $A1 = $FQDN.Split(".") $HostName = $A1[0] } ELSE { $HostName = "HostName NOT FOUND" } ##Get MAC and Host from nbtstat IF($strNBT -like ‘Host not found’) { $MAC = "MAC NOT FOUND" $NBTHost = "NBT HOST NOT FOUND" } ELSE { $A1 = $strNBT.Split("=") $S1 = $A1[1] $A2 = $S1.Split(" ") $PreMAC = $A2[1] $MAC = $PreMAC.Replace("-",":") $A3 = $strNBT.Split(" ") $S2 = $A3[73] $A4 = $S2.Split("<") $NBTHost = $A4[0] } $all="$IPt$NSHostt$NBTHostt$MACt$FQDN" write-host "$all" }}Start-Sleep 5Start-Job -ScriptBlock $job3 -ArgumentList (,$Range3)$job4 = { param($Range4) ($Range4) | ForEach-Object -Process { $IP = "$" ##Run Commands to Pull Info From and assign output to String $NSLookup = nslookup $IP $strNS = "$NSLookup" $NBTstat = nbtstat -A $IP $strNBT = "$NBTstat" ##Get FQDN from nslookup IF($strNS -like ‘Name’) { $A1 = $strNS.Split(":") $S1 = $A1[3] $A2 = $S1.Split(" ") $FQDN = $A2[4] } ELSE { $FQDN = "FQDN NOT FOUND" } ##Get Hostname from FQDN IF($strNS -like ‘Name’) { $A1 = $FQDN.Split(".") $HostName = $A1[0] } ELSE { $HostName = "HostName NOT FOUND" } ##Get MAC and Host from nbtstat IF($strNBT -like ‘Host not found’) { $MAC = "MAC NOT FOUND" $NBTHost = "NBT HOST NOT FOUND" } ELSE { $A1 = $strNBT.Split("=") $S1 = $A1[1] $A2 = $S1.Split(" ") $PreMAC = $A2[1] $MAC = $PreMAC.Replace("-",":") $A3 = $strNBT.Split(" ") $S2 = $A3[73] $A4 = $S2.Split("<") $NBTHost = $A4[0] } $all="$IPt$NSHostt$NBTHostt$MACt$FQDN" write-host "$all" }}Start-Sleep 5Start-Job -ScriptBlock $job4 -ArgumentList (,$Range4) While (Get-Job -State "Running") {Start-Sleep 30}Write-Host "IPtDNS_HostNametNetBIOS_HostNametMAC_AddresstFully_Qualified_Domain_Name"#Display output from all jobsGet-Job | Receive-Job`

by DexterPOSH at 2012-11-14 03:39:40

> Hi Justin,  
>   
> When you use Receive-Job cmdlet once, the results are flushed out from the memory. In order to keep them you can use the -keep parameter of the cmdlet and then I think when you run the cmdlet second time it would display the result.  
> I saw your previous post and I think it works fine for you 🙂  
>   
> A friendly advice :You could use function to do the repeated task again and again.   
>   
> Hope it makes sense

by the\_hutch at 2012-11-18 18:14:57

> It wasn’t actually a problem with seeing the output when using Receive-Job. It was that I couldn’t assign the output to a variable, even if it was the first time using it. I figured it out though. Instead of using write-host in each job, I had to use write-output. But using that, it allowed my to assign the output to a variable with:   
> `$A = (Get-Job | Receive-Job)`  
>   
> And thanks for the tip. I did a lot of cleanup afterwards.

by DonJ at 2012-11-19 07:21:51

> Write-Host, generally speaking, is always bad. 🙂

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 9:02pm UTC](https://forums.powershell.org/t/saving-the-output-of-receive-job-to-variable/329/2 "2024-05-16T21:02:16Z")

</div>


