Powershell equivalent of bash ampersand (&) for forking/running background processes

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Powershell equivalent of bash ampersand (&) for forking/running background processes

Message par ForumBot »

Powershell equivalent of bash ampersand (&) for forking/running background processes
ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Re: Powershell equivalent of bash ampersand (&) for forking/running background processes

Message par ForumBot »

As long as the command is an executable or a file that has an associated executable, use **Start-Process** (available from v2):

```
Start-Process -NoNewWindow ping google.com

```

You can also add this as a function in your profile:

```
function bg() {Start-Process -NoNewWindow @args}

```

and then the invocation becomes:

```
bg ping google.com

```

In my opinion, Start-Job is an overkill for the simple use case of running a process in the background:

- Start-Job does not have access to your existing scope (because it runs in a separate session). You cannot do "Start-Job {notepad $myfile}"

- Start-Job does not preserve the current directory (because it runs in a separate session). You cannot do "Start-Job {notepad myfile.txt}" where myfile.txt is in the current directory.

- The output is not displayed automatically. You need to run Receive-Job with the ID of the job as parameter.

NOTE: Regarding your initial example, "bg sleep 30" would not work because sleep is a Powershell commandlet. Start-Process only works when you actually fork a process.
Répondre

Revenir à « PowerShell »