# Launching PS in background on a target host

**URL:** <https://forums.powershell.org/t/launching-ps-in-background-on-a-target-host/9648>\
**Category:** PowerShell Help\
**Created:** [November 3, 2017, 10:57am UTC](https://forums.powershell.org/t/launching-ps-in-background-on-a-target-host/9648 "2017-11-03T10:57:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![marius1956](https://avatars.discourse-cdn.com/v4/letter/m/eada6e/32.png) [@marius1956](https://forums.powershell.org/u/marius1956)\
**Post date:** [November 3, 2017, 10:57am UTC](https://forums.powershell.org/t/launching-ps-in-background-on-a-target-host/9648/1 "2017-11-03T10:57:44Z")

</div>

I have a PowerShell script that issues a command like:

```
$my_exit = invoke-Command -Session $my_session -ScriptBlock { ... } -Args
```

to execute a long script on a target server.  
If the calling script terminates for any reason (maybe, the PC is powered off due to a power failure) the script on the target host terminates as well.  
How can I launch a script on a target host so that it continues even if the calling script closes?  
I made a test adding the -asjob qualifier with no success.  
Regards  
marius

---

<div class="post-metadata">

**Author:** ![alex-aymonier](https://avatars.discourse-cdn.com/v4/letter/a/ecd19e/32.png) [@alex-aymonier](https://forums.powershell.org/u/alex-aymonier)\
**Post date:** [November 5, 2017, 8:24pm UTC](https://forums.powershell.org/t/launching-ps-in-background-on-a-target-host/9648/2 "2017-11-05T20:24:16Z")

</div>

Try this:

Run the remote scripts on a remote computer with a persistent session

```
$session = New-PSSession -computername "FQDN" -credential Get-Credential
Invoke-Command -session $session -ScriptBlock {E:\Testing.ps1 "param1"}
```

---

<div class="post-metadata">

**Author:** ![pradeeparoratx](https://avatars.discourse-cdn.com/v4/letter/p/f6c823/32.png) [@pradeeparoratx](https://forums.powershell.org/u/pradeeparoratx)\
**Post date:** [November 7, 2017, 10:24am UTC](https://forums.powershell.org/t/launching-ps-in-background-on-a-target-host/9648/3 "2017-11-07T10:24:14Z")

</div>

To add to the answer, you could try this:

```
$session = New-PSSession -computername "FQDN" -credential Get-Credential
Invoke-Command -session $session -ScriptBlock {start cmd.exe "/k powershell.exe ./DoThis.ps1"}
```

---

<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, 8:37pm UTC](https://forums.powershell.org/t/launching-ps-in-background-on-a-target-host/9648/4 "2024-05-16T20:37:16Z")

</div>


