Which environment variables are available inside a remote desktop (RDP) session?

Posted: (EET/GMT+2)

 

You've probably used the Remote Desktop Protocol (RDP) numerous times, but sometimes, you might need to get a little extra information from the session.

For example, when working with custom .NET applications, you might want to know whether the current user is accessing your application through a remote desktop connection, and what is the current user's client computer name and the session identifier.

Although there's a native Win32 API just for this, you can also get basic information using regular environment variables. These are easy to access from C#, and thus serve as a quick way to get things going.

Specifically, all the normal environment variables like PATH, TEMP and WINDIR are there, but RDP sessions have two special variables: CLIENTNAME and SESSIONNAME.

Although these two variable aren't well documented, they work all the way from Windows XP and Windows Server 2002 (!) to the latest Windows Server 2016 Technical Previews at this writing. The CLIENTNAME variable holds the name of the computer accessing the remote computer, and SESSIONNAME is an identifier for the session, such as "RDP-Tcp#2".

These two environment variables are actually read from the registry during logon. The registry location is:

HKEY_CURRENT_USER\Volatile Environment

Below this key, there's another subkey with a simple number like "2" to identify the session number.

Hope this helps!