Exit-PSSession

End an interactive session with a remote computer.

Syntax
      Exit-PSSession [CommonParameters] ErrorLevel

Key
   CommonParameters:
       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
       -OutBuffer -OutVariable.

Standard Aliases for Exit-PSSession: exsn

To end the interactive session and disconnect from the remote computer, use Exit-PSSession, or type "exit".

Examples

Start and then stop an interactive session with the Server64 remote computer:

PS C:> Enter-PSSession -computername Server64
Server01\PS> Exit-PSSession

Start and stop an interactive session with Server64, the status of the PSSession shows that the session is still available when the interactive session ends.:

PS C:> $sess = new-pssession -computername Server64
C:\PS> Enter-PSSession -session $sess
Server01\PS> Exit-PSSession
C:\PS> $sess

Id Name ComputerName State ConfigurationName
-- ---- ------------ ----- -----------------
1 Session1 Server64 Opened Microsoft.PowerShell

Note that if Enter-PSSession -ComputerName is used (instead of new-psSession), then Enter-PSSession would have automatically created a temporary session that would close as soon as the interactive session ends.

Start PowerShell.exe and run a .ps1 script that will exit returning an ErrorLevel back to the calling shell (in this case CMD.exe)

C:\> powershell.exe -noprofile –file "C:\bats\DemoScript.ps1"
:: DemoScript.ps1ends with the line: Exit-PSSession 64
C:\> Echo %ERRORLEVEL%
64

“Every exit is an entrance somewhere else” ~ Tom Stoppard

Related:

Enter-PSSession - Start an interactive session with a remote computer
Remove-PSSession - Close PowerShell sessions (non-interactive)


© Copyright SS64.com 1999-2014
Some rights reserved