2

I want to retrieve the full name of a Windows computer user in Python.

I have found the equivalent batch command:

net user "%USERNAME%" /domain | FIND /I "Full Name"

that returns the full name (e.g. Full Name John Doe).

I have done the following way by using subprocess but I am wondering if there is a more native way to do it with some Python modules.

import getpass
import subprocess
import re

username = getpass.getuser()
p = subprocess.Popen(
    'net user %s /domain' % username,
    stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
info, err = p.stdout.read(), p.stderr.read()
full_name = re.findall(r'Full Name\s+(.*\S)', info)

print(full_name)

Thanks

2

The more direct way to do this is to query Active Directory. You'd perform a lookup on the user, followed by getting the displayName attribute. (This maps to the Full Name displayed in Windows.)


You have two options here:

Using a Python AD library, e.g. pyad

This is very Windows-specific, and requires the pywin32 library. It relies on ADSI APIs, so will only work on Windows.

from pyad import aduser
user = aduser.ADUser.from_cn(username)
print user.get_attribute("displayName")

How you get the username is up to you. You can use getpass.getuser(), os.environ["USERNAME"] (Windows-only), etc.

Using a Python LDAP library, e.g. ldap3

This follows the standard LDAP protocol, with a pure Python implementation, so should work from any client OS.

Using raw LDAP queries is rather more involved than the ADSI abstractions. I suggest you read the documentation (which has decent tutorials) and search for more tutorials on interacting with Microsoft AD via ldap3.


Note that one possible issue is that searching by username (CN) alone might get you the wrong object. It's possible to have multiple objects with the same CN across multiple OUs. If you want to be more precise, you might want to use a unique identifier like the SID.

  • Is there a method using Windows' "Net*()" functions? (Or, well, similar.) It would then work for any kind of account – local, nt4, ad... – grawity Aug 11 '17 at 6:55
  • @grawity Hm, I'd not even thought of that. With pywin32 you could use import win32net and use win32net.NetUserGetInfo(), which calls NetUserGetInfo(). A level of 2 or higher includes a full_name. However, with a bit of testing, it appears that you have to specify the server to get a domain response anyway - querying a domain user (even the currently logged in one) against None/localhost fails. – Bob Aug 11 '17 at 7:19
  • Interesting answer, although I have an error while trying with pyad when doing aduser.ADUser.from_cn: The specified query resturned 0 results. getSingleResults only functions with a single result.. I believe I will stick to my ugly solution :) – Jean-Francois T. Aug 14 '17 at 3:04
  • @Jean-FrancoisT. Hm, that's interesting - it worked when I tested. I assume the machine you're running it on is joined to the domain, and that you are using a correct username? It should work with the same thing you pass into net user /domain. Failing that, you could try the NetUserGetInfo method. Or your subprocess method if you can accept the ugliness ;) – Bob Aug 14 '17 at 4:08

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.