PowerShell Remote – Start Command and Have It Finish Even If Disconnected

powershell

If I start a remote PowerShell session with:

Enter-PSSession -ComputerName somecomputer

Then I want to execute a long-running process:

[somecomputer]: PS C:\> C:\SomeApp\DoSomething.exe

If my remote session is disconnected for whatever reason – such as a local machine network outage or reboot – is there any way to ensure that the command still completes on the server?

From what I'm seeing, it seems to go away as soon as my PS session dies. I've also tried Start-Process, but it seems to behave the same.

I'm trying to run things on a server without having to RDP in. Still a beginner in PowerShell, so I'm sure there are plenty of things I'm missing. I'm on Win10, PowerShell 7, connecting to Windows Server 2016, but I assume it would be the same anywhere.

Best Answer

I think what you're looking for is New-PSsesion

Enter-PSSession: Starts a temporary interactive session with a remote computer. You can have only one interactive session at a time using Enter-PSSession. Exiting the session destroys it.
New-PSSession: Creates a PERSISTENT connection to a local or remote computer. Exiting the session does not destroy it, it is still available to connect to again unless you do something like Disconnect-PSSession or it times out.
Furthermore, with New-PSSession you can assign the session a Name or to a variable for easier re-use, etc.