tags:

views:

3214

answers:

3

In bash the ampersand (&) can be used to run a command in the background and return interactive control to the user before the command has finished running. Is there an equivalent method of doing this in Powershell?

Example of usage in bash:

 sleep 30 &
A: 

It "just does" :)

PowerShell directly runs executables. If it waits for the process to exit, then the exit code left in $LASTEXITCODE. Now if a Win32 executable is the last command in a pipeline and is not redirected, then the process will run in the background. However, if you pipe its output into something, then PowerShell will wait for the command to exit.

From here with examples and details.

If you need things to run sequentially like I've run into before- pipe the output to out-null so you don't return control to the script before the pipeline finishes.

foo.msi | out-null

If you are trying to run a cmdlet, or script in the background then see the other great answers, but for executables, they are already async.

slipsec
I'm afraid this doesn't answer his question about background jobs.
halr9000
+8  A: 

The are a few answers to this question.

  1. You can't easily do it in version 1
  2. Version 2 (now in community tech preview 2) does have this feature, it's called a PSJob. Find more out about this here or here.
  3. You can actually do it the hard way in v1, feel free to have at it but I've never bothered.
halr9000
+2  A: 

I've used the solution described here http://jtruher.spaces.live.com/blog/cns!7143DA6E51A2628D!130.entry successfully in PowerShell v1.0. It definitely will be easier in PowerShell v2.0.

Jeffery Hicks