As developers working with remote systems, we often face session management issues when using Microsoft Terminal Services Client (MSTSC). The random behavior of either reconnecting to existing sessions or creating new ones can disrupt workflows and leave orphaned processes.
The Remote Desktop Protocol (RDP) client typically follows these connection rules:
- Attempts to reconnect to disconnected sessions by default (but not always reliably)
- Creates new sessions when no matching credentials/session ID is found
- May be affected by server-side Group Policy settings
For precise control over session reconnection, use these MSTSC command-line switches:
mstsc /v:server.domain.com /admin /f /console
Key parameters:
/v:[server] - Specifies the remote computer /admin - Connects to the console session (Windows Server) /console - Legacy switch (pre-Windows Server 2008) /f - Starts in full-screen mode
Create a PowerShell script to ensure consistent reconnection behavior:
$server = "your-server-address" $existingSession = qwinsta /server:$server | Select-String "YourUsername" if ($existingSession) { $sessionID = ($existingSession -split '\s+')[2] mstsc /v:$server /shadow:$sessionID /control /noConsentPrompt } else { mstsc /v:$server }
Modify these registry settings on the client machine:
Windows Registry Editor Version 5.00 [HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client] "DisableConnectionSharing"=dword:00000000
For developers needing to check active sessions before connecting:
using System; using System.Diagnostics; class RdpSessionManager { static void Main() { var psi = new ProcessStartInfo { FileName = "cmd.exe", Arguments = "/c qwinsta /server:yourserver | find \"Active\"", RedirectStandardOutput = true, UseShellExecute = false }; var process = Process.Start(psi); string output = process.StandardOutput.ReadToEnd(); // Parse output to identify your session } }
Administrators can enforce session reconnection policies via Group Policy:
Computer Configuration > Administrative Templates > Windows Components > Remote Desktop Services > Remote Desktop Session Host > Connections "Restrict Remote Desktop Services users to a single Remote Desktop Services session" - Disabled
To clean up disconnected sessions remotely:
logoff [sessionID] /server:yourserver
Or using PowerShell Remoting:
Invoke-Command -ComputerName yourserver -ScriptBlock { Get-RDUserSession | Where {$_.SessionState -eq "Disconnected"} | Disconnect-RDUserSession -Force }
As a developer working extensively with remote systems, I've encountered the same frustrating behavior where Microsoft Terminal Services Client (MSTSC) sometimes creates new sessions instead of reconnecting to existing ones. This becomes particularly problematic when:
- Running long-term processes that can't be interrupted
- Maintaining development environment states
- Preserving unsaved work in IDE instances
The Windows Remote Desktop Service manages sessions using Session IDs. When you disconnect (not log off), your session remains active on the server. By default, MSTSC attempts to reconnect to your existing session, but several factors can prevent this:
// Sample PowerShell to check active sessions
query session /server:yourserver
Here are three technical approaches to ensure MSTSC reconnects to your existing session:
1. Command Line Parameters
The most reliable method is using MSTSC's command line switches:
mstsc /v:yourserver /admin /f /shadow:1 /noConsentPrompt
Key parameters:
/admin
- Connects to console session/shadow
- Controls session reconnection behavior
2. Registry Modification
For a system-wide setting, modify the registry:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Terminal Server Client]
"DisableConnectionSharing"=dword:00000000
3. Using RDP File Configuration
Create or modify your RDP connection file with these settings:
screen mode id:i:2
use multimon:i:0
session bpp:i:32
winposstr:s:0,1,781,243,1680,963
compression:i:1
keyboardhook:i:2
audiocapturemode:i:0
videoplaybackmode:i:1
connection type:i:7
networkautodetect:i:1
bandwidthautodetect:i:1
displayconnectionbar:i:1
enableworkspacereconnect:i:1
disable wallpaper:i:0
allow font smoothing:i:1
allow desktop composition:i:0
disable full window drag:i:1
disable menu anims:i:1
disable themes:i:0
disable cursor setting:i:0
bitmapcachepersistenable:i:1
autoreconnection enabled:i:1
For developers who need programmatic control, here's a PowerShell script to ensure session reconnection:
function Connect-RemoteDesktop {
param (
[string]$Server,
[int]$SessionID
)
$rdpPath = "$env:TEMP\AutoReconnect.rdp"
$connectionSettings = @"
full address:s:$Server
audiomode:i:0
redirectprinters:i:1
redirectcomports:i:1
redirectsmartcards:i:1
redirectclipboard:i:1
redirectposdevices:i:0
autoreconnection enabled:i:1
authentication level:i:2
connect to console:i:1
"@
$connectionSettings | Out-File -FilePath $rdpPath
Start-Process mstsc -ArgumentList $rdpPath
}
# Usage example:
# Connect-RemoteDesktop -Server "dev-server-01" -SessionID 2
When these methods don't work, consider:
- Server-side session limit configurations
- Group Policy settings overriding your preferences
- Network-level interruptions causing session timeouts
For complete control, combine these techniques with server-side configuration adjustments in the Remote Desktop Session Host Configuration settings.