Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

how do I do a background task in Powershell?

$ ant >/dev/null &        #cygwin

The closest I can get to this in Powershell is

ps2> start-job {ant |out-null; `a}   #powershell

but then I don't get output in real-time, nor do I get the beep. I have to poll for it with receive-job and get the beep when stdout gets polled.

share|improve this question

Hopefully you are on Powershell Version 2. If you are then this is how to background process a job:

Start-Job {Get-Process}

That will run the job in the background. If you want to interact with the job, then just assign it to a variable:

$foo = Start-Job {Get-Process}

Now of course the trickiness comes with knowing when the job is done. I realize that is the purpose of the bell that you put in your original example. Unfortunately Powershell holds the pipe until you ask for the results from the job. To get the results from the job use the Receive-Job cmdlet:

Receive-Job $foo

I am interested in a way to alert when a background job has completed as well so I will keep looking for a solution to that.

Oh and for more info:

Get-Help about_jobs

Ok, so here is how to get a beep on completion. First, read this Get Notification When A Background Job Is Done (that is a great resource site by the way). I took the info there and I added this function to my profile:

function Register-JobWait{
Register-ObjectEvent $job StateChanged -Action {
$eventSubscriber | Unregister-Event
$eventSubscriber.Action | Remove-Job
} | Out-Null


I also added an alias in my profile:

set-alias rjw Register-JobWait

so now if I want to background process something I do this:

$foo = Start-Job {1..10 | Get-Process}
rjw $foo

I wait for the beep then:

Receive-Job $foo
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .