2
votes

When I execute $job = Start-Job { dir } an then Receive-Job $job in PowerShell console I get normal output. But when I make similar .ps1 script and run it there is no output. Other commands work correctly. How do I receive job result in scripts?

2

2 Answers

3
votes

Just try wait job is completed before receive.

$job = Start-Job { dir }
Wait-Job $job | out-null
receive-job $job

other way

$job = Start-Job { dir }
while ($job.state -ne "Completed") {}
receive-job $job
2
votes

You need to wait for the job to finish:

Start-Job { dir } | Wait-Job | Receive-Job