How can I get the IP address of a remote desktop client?


How can I get the IP address of a remote desktop client? (Powershell - running in users context - non-admin)

Thanx in advance

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SilverwolfSenior Server EngineerCommented:
You can use the following commands. The latter can only be done in PowerShell.

Command Prompt or Powershell: ping -a IPADDRESS

PowerShell: [System.Net.Dns]::GetHostbyAddress("IPAddress")
Spike99On-Site IT TechnicianCommented:
I think the author is asking how to get the IP of the remote PC from the remote desktop session on the server. Those 2 commands require the IP of the machine in order to run.

PING -A IPADDRESS, for example,  is a reverse lookup.  It will give you the name of the system if you know the IP, but he doesn't know the ip.
SilverwolfSenior Server EngineerCommented:
Oops, your right. My bad. Multi-tasking too much. lol

In that case just run the ping command with the hostname (Computer name):
ping hostname.
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Dan CraciunIT ConsultantCommented:
netstat should give you all established connections. The IPs connected on port 3389 should be your remote desktop clients.

Spike99On-Site IT TechnicianCommented:
Try running this at the command prompt.  It should bring up the client name & IP:

ping %clientname%

Open in new window

I ran that in RDP sessions on both Windows 7 and Server 2008 R2: it pulled up the client name & IP for me on both.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
This'll get you the client IP of the current user's session. Can't test on IPv6, though, so you might have to play with the copy start index in line 54 if the address format isn't correct if you're using IPv6. Should work on XP and PS 2.0 or later.
If (-not ('WTSApi' -as [Type])) {Add-Type -Language CSharp -TypeDefinition @'
using System;
using System.Runtime.InteropServices;

public class WTSApi
	static extern void WTSFreeMemory(IntPtr pMemory);

	static extern bool WTSQuerySessionInformation(System.IntPtr hServer, int sessionId, WTS_INFO_CLASS wtsInfoClass, out System.IntPtr ppBuffer, out uint pBytesReturned);

	private struct WTS_CLIENT_ADDRESS
		public int AddressFamily;
		[MarshalAs(UnmanagedType.ByValArray, SizeConst=20)]
		public byte[] Address;

	private enum WTS_INFO_CLASS
		WTSClientAddress = 14

	private enum ADDRESSFAMILY
		AF_UNSPEC = 0,
		AF_INET = 2,
		AF_IPX = 6,
		AF_NETBIOS = 17,
		AF_INET6 = 23
	public static string GetSessionIP()
		IntPtr WTSClientAddressPtr = IntPtr.Zero;
		string ClientAddress = "";
		uint bytes = 0;
		WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, WTS_CURRENT_SESSION, WTS_INFO_CLASS.WTSClientAddress, out WTSClientAddressPtr, out bytes);
		WTS_CLIENT_ADDRESS WTSClientAddress = (WTS_CLIENT_ADDRESS)Marshal.PtrToStructure(WTSClientAddressPtr, typeof(WTS_CLIENT_ADDRESS));
				ClientAddress = WTSClientAddress.Address[2] + "." + WTSClientAddress.Address[3] + "." + WTSClientAddress.Address[4] + "." + WTSClientAddress.Address[5];
				byte[] IPv6 = new byte[16];
				Array.Copy(WTSClientAddress.Address, 0, IPv6, 0, 16);	// ATTENTION: Microsoft does not document WHERE the 16 bytes of the IPv6 address are located in the 20 byte array (IPv4 starts at byte 2, not 0)!
				ClientAddress = BitConverter.ToString(IPv6);
				ClientAddress = BitConverter.ToString(WTSClientAddress.Address);
		return ClientAddress;

Open in new window

Senior IT System EngineerSenior Systems EngineerCommented:
Thanks oBdA,

The solution works :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.