How to keep remote powershell command alive after session end? How to keep remote powershell command alive after session end? powershell powershell

How to keep remote powershell command alive after session end?


The way to keep the remote PowerShell session running after the command has finished is to use a PSSession e.g.:

$s = new-PSSession computernameInvoke-Command -session $s { ..script.. }... do other stuff, remote powershell.exe continues to runRemove-PSSession $s # when you're done with the remote session

Generally though exes should run independently from the app that launched them.


Why are you using Invoke-Command. If you want a persistent Session, use Enter-PSSession.

$s = New-PSSession -Computername "Computername";Enter-PSSession -Session $s;setup_server.exe# Once you are finnishedExit-PSSession

With 'Enter-PSSession' you are not just Invoking some Command on the Server, you are directly logged-in like you probably know from SSH.


If you want your powershell session to keep running because you are running an exe, try using the -InDisconnectedSession switch. From what I understand, it will run the executable on the remote machine in a session that isn't actually connected to your computer. In essence, your computer will not destroy the session, when it disconnects, allowing the exe to continue to run.

invoke-command -computername RCOMPUTERNAME -scriptblock {start-process setup_server.exe} -InDisconnectedSession

If you need to do this on multiple computers. Setup an array of all the computer names.

Note: I don't believe this works with sessions that are already created.