PowerShell Enter-PSSession: Interactive Remote Shell

PowerShell Enter-PSSession: Interactive Remote Shell

PowerShell Tips Editor 4 min read
PowerShell Enter-PSSession: Interactive Remote Shell

Sometimes you need to explore a remote server interactively — check a config file, tail a log, test a cmdlet — rather than just run a predefined script block. PowerShell Enter-PSSession opens an interactive PowerShell prompt on a remote computer, giving you a live shell where every command runs there, not here. It’s the PowerShell equivalent of SSH for Windows, and knowing when to use it versus Invoke-Command is essential for effective remote administration.

Enter-PSSession Basic Syntax

The minimum required parameter is -ComputerName. PowerShell remoting must already be enabled on the target (Enable-PSRemoting -Force on the remote machine). Once you enter the session, your prompt changes to reflect the remote computer name.

# Enter an interactive session
Enter-PSSession -ComputerName "server01"
[server01]: PS C:\Users\Administrator>

Every command you type now executes on server01. Type Exit-PSSession or press Ctrl+C followed by exit to return to your local session.

Using Credential Parameter

When connecting to a machine in a different domain, a workgroup machine, or as a different user, supply credentials with -Credential. Get-Credential prompts for a username and password and returns a PSCredential object.

$cred = Get-Credential -Message "Enter admin credentials for server01"
Enter-PSSession -ComputerName "server01" -Credential $cred

You can also pass a credential object you created earlier in the script, so the prompt only appears once even if you’re opening multiple sessions.

# Credential from stored variable
$cred = Get-Credential "DOMAIN\adminuser"
Enter-PSSession -ComputerName "server02" -Credential $cred

Working Inside the Remote Session

Inside the remote session, all commands run on the remote computer. Local variables, paths, and modules are those of the remote machine — not your local system. You can run any PowerShell command or executable that exists on the remote host.

[server01]: PS C:\> Get-Service | Where-Object Status -eq "Stopped"
[server01]: PS C:\> Get-ChildItem C:\Logs -Filter "*.log" | Sort-Object LastWriteTime -Descending | Select-Object -First 5
[server01]: PS C:\> $env:COMPUTERNAME   # Returns "server01"
[server01]: PS C:\> Test-Path "C:\App\config.xml"

Remember: the file system paths you reference are on the remote machine. C:\Logs refers to server01‘s C drive, not yours.

Exit-PSSession Cleanly

Always exit the remote session properly to release the WinRM connection. An abrupt close can leave the session in a disconnected state on the server, consuming resources.

[server01]: PS C:\> Exit-PSSession

# Back to local prompt:
PS C:\>

The prompt reverts to your local machine. If you accidentally close the terminal window instead of exiting, the session will eventually time out based on the WinRM idle timeout (default 7.5 minutes).

Enter-PSSession vs Invoke-Command

The two tools serve different purposes:

  • Enter-PSSession — interactive, one machine at a time, for exploration and debugging
  • Invoke-Command — non-interactive, multiple machines at once, for automation and scripting
# Enter-PSSession: interactive debugging
Enter-PSSession -ComputerName "server01"

# Invoke-Command: run same block on 10 servers simultaneously
Invoke-Command -ComputerName $serverList -ScriptBlock { Get-Service "Spooler" }

Use Enter-PSSession when you don’t yet know exactly what commands you need to run. Use Invoke-Command when you do.

Reconnect to Disconnected Sessions

If a network glitch drops your interactive session mid-task, the session may remain alive on the remote server in a Disconnected state. You can reconnect to it rather than starting fresh, which is valuable when a long-running command is still executing on the remote end.

# List all sessions — including disconnected ones
Get-PSSession -ComputerName "server01"
Id  Name       ComputerName  State        ConfigurationName
--  ----       ------------  -----        -----------------
1   Session1   server01      Disconnected Microsoft.PowerShell
# Reconnect to the disconnected session
$session = Get-PSSession -ComputerName "server01" | Where-Object State -eq "Disconnected"
Connect-PSSession -Session $session
Enter-PSSession -Session $session

Common Errors and Fixes

  • Prompt prefix confusion: Once inside a remote session, the prompt shows [server01]: PS C:\> — but it’s easy to forget and accidentally run a destructive command thinking you’re still local. Pay attention to the prefix at all times. Consider adding a visible color change to your prompt function when in a remote session by checking $PSSenderInfo.
  • Session disconnects on network glitch: For long-running tasks over unreliable connections, avoid Enter-PSSession and use Invoke-Command with a persistent New-PSSession instead. If you do use interactive sessions for lengthy work, consider running your commands inside a Start-Job on the remote machine so the work continues even if you disconnect.

Related Cmdlets / See Also

Wrapping Up

Enter-PSSession is your interactive remote shell for Windows — perfect for exploration and debugging when you need to feel out what’s happening on a remote server. Use it to discover the right commands, then graduate those commands into Invoke-Command script blocks for repeatable, scalable automation.

Send-Item -To