Get windows domain user

Find domain name from command line

We can find the domain name of a computer by running the following commnad from command line.

We can find the logged in user’s domain by using the environment variable ‘USERDOMAIN’. Command for this is given below.

Note that the value in %USERDOMAIN% may not be the same as the one returned by systeminfo command. %USERDOMAIN% gives the domain name the user account belongs to, it could be different from the domain of the computer. Also, this may give you the NetBios name of the computer, not DNS/FQDN name.

Alternatively, we can use WMIC to retrieve domain name.

Whatt is the command to view domain name in windows 7

All the above commands work on Windows 7. WMIC one may/may not work depending on whether the edition of win7 you have supports it or not.

The systeminfo command given in this post works on Windows 7 also. I think copying the command from this page had some problem and it did not work. Have corrected it. It should work now.

systeminfo | findstr Domain

I see no reason for the /b /c:” “

/C may not be required. /b is required to avoid matches with the values for other fields of systeminfo command.

Thanks! Useful!! Keep it up your good work.

Thanks for this! I needed to pull the domain for a powershell script for use on servers that didn’t have the ActiveDirectory module (and I wasn’t going to install it).

This enabled me to grab the domain, then trim out the header and return in Powershell for my variable.

This command returns domain or workgroup: net config workstation
* If domain: “Workstation Domain DNS Name”
* If group: “Workstation domain”

Get-WmiObject is not recognized as a command. Any help guys?

Get-WmiObject is a PowerShell cmdlet. You need to use the command in the PowerShell Terminal. To do it in the CMD Terminal, you need to run the WMIC commands (as explained above)
For example, “wmic computersystem get domain” (without quotes)

Can someone please guide me on how to obtain the domain name of the servers remotely. I need to get the domain name for hunderds of servers.
Is there a way to create a CSV file with the server names and create a batch file and run it to obtain this information?
Any help would be highly appreciated.
Thanks.

wmic /node:”COMPUTERNAME” computersystem get domain

I am getting domain as workgroup. what’s wrong here?

It means that your computer is not part of a domain but a workgroup. All the home computers are part of a workgroup.

what is domain for windows 8? i need it for change to administrator, it is will work?

thanks for share me the value knowledge

Excellent info and list of commands.. Thank you.

Just type –> set user

gives you the domain info without searching

set user only shows the domain of which the user is a member.
NOT the computer’s domain. (This could be different).

If so, the ‘wmic computersystem get domain’ is a working command.

Читайте также:  Планшет за базе windows

systeminfo | findstr /B /C:”Domain” depends on the OS language.
if you have a german OS, it’s
systeminfo | findstr /B /C:”Domäne” .
But otherwise fine info.

Get windows domain user

This forum has migrated to Microsoft Q&A. Visit Microsoft Q&A to post new questions.

Answered by:

Question

How can I determine if the currently logged in windows user account is from a Local machine account or from the Active Directory domain account?

My C# windows application need to know this info to set some application previleges to the user based on how he/she is currently logged in.

Answers

How can I determine if the currently logged in windows user account is from a Local machine account or from the Active Directory domain account?

use echo %logonserver% command and check the output. If it is the local machine then you are using a local account and if it is a DC that is mentioned then you are using a domain user.

Another option is to use whoami command and:

  • If you are logged using a local account then you will get as a result Computer\username
  • If you are logged using a domain account then you will get as a result Domain\username

My C# windows application need to know this info to set some application previleges to the user based on how he/she is currently logged in.

This posting is provided «AS IS» with no warranties or guarantees , and confers no rights.

Microsoft Student Partner 2010 / 2011
Microsoft Certified Professional
Microsoft Certified Systems Administrator: Security
Microsoft Certified Systems Engineer: Security
Microsoft Certified Technology Specialist: Windows Server 2008 Active Directory, Configuration
Microsoft Certified Technology Specialist: Windows Server 2008 Network Infrastructure, Configuration
Microsoft Certified Technology Specialist: Windows Server 2008 Applications Infrastructure, Configuration
Microsoft Certified Technology Specialist: Windows 7, Configuring
Microsoft Certified IT Professional: Enterprise Administrator
Microsoft Certified IT Professional: Server Administrator
Microsoft Certified Trainer

Get display name of current Windows domain user from a command prompt

From the command prompt, how can I get the friendly display name (that is, «John Doe» instead of «john.doe») of the domain user that is currently logged in?

8 Answers 8

Here is a tricky way that I did it using the net command and the find command in a batch file:

The first line stores the command that we want to execute in a variable. It pulls the username from the environment variables and passes that into the net user command as well as the /domain parameter to tell it to pull from the current domain. Then it pipes the result from that, which is a bunch of data on the current user, to a find method which will pull out only the property that we want. The result of the find is in the format «Full Name John Doe». The second line will execute the command and put the result into the variable fullNameText . The third line will remove the «Full Name» part of the result and end up with » John Doe». The fourth line with the for loop will remove all of the leading spaces from the result and you end up with «John Doe» in the fullName variable.

Lectrode answer in one string will be like this:

Here’s a derivative of skeletank’s response that’s slightly cleaner that worked for me. (I tried skeletank’s response and it didn’t work for me the way it was written, thus my answer below.)

Читайте также:  Tar linux ��� �������������� �����

Then you can use ‘DNAME’ anywhere you need like to ouput with an echo or validate with an ‘IF’ statement. You can flip the ‘%%B’ and ‘%%A’ tokens around to go back to last name first layout. This saves the additional effort used in skeletank’s original response.

Variables:
TNAME = Temporary Name
DNAME = Display Name

This should be a clean way using dsquery and dsget without relying on the output of «Full Name», which is language dependent, whereas «fn» seems to work with different language versions of windows.

The dsget -L switch gives the listed output «fn: USER» so the only thing left is to grab the second column in the output. find «fn:» is used to get rid of the «dsget succeeded» output.

The accepted answer (at time of posting) has 4 lines of code, when you only need one to do the same thing:

This will work even if the Full Name has more than 2 parts. Works on Windows workstations «out of the box» and does not require admin rights.

NOTE: Remember to change %n and %o to %%n and %%o ,respectively, when using in a batch file.

Explanation of the command:

«tokens=2* specifies that the only tokens it will pay attention to are the 2nd token (assigned to %n ), and everything after it (assigned to the next letter: %o )

The default delims is space so we don’t need to define it, but if we need to manually define it it’d be as follows: «tokens=2* delims= » . Because the space is a delimiter, it will ignore all leading spaces.

The pipe ( | ) is escaped by a carrot ( ^ ), telling the command processor to ignore it initially.

I also prefer to use FINDSTR instead of FIND because (in my testing) I’ve found it to be the faster command, which can make a difference if you’re using it frequently. FINDSTR /C:»Full Name» could be easily replaced with FIND «Full Name» if you preferred.

How to get Windows domain name?

How can i get the domain name of the machine (if the machine is in fact joined to a domain)?

And, of course, if the machine is not joined to a domain the function should return

  • null , or
  • an empty string, or
  • the name of the machine, or
  • «.»

Notes:

the NetGetJoinInformation Win32 function returns the legacy NetBIOS name of the domain (e.g. AVATOPIA ), not the name of the domain (e.g. avatopia.local )

the USERDOMAIN environment variable returns the domain of the logged on user, which can be different from the machine; and also returns the legacy NetBIOS name of the domain (e.g. AVATOPIA )

the USERDNSDOMAIN environment variable returns the domain name of the logged on user, which can be different from the machine

Microsoft has a knowledge base article How to retrieve current user and domain names on Windows NT, Windows 2000, or Windows XP, which relies on getting the user’s security token and calling LookupAccountSid.

  • the LookupAccountSid Win32 function returns the legacy NetBIOS name of the domain (e.g. AVATOPIA ); and also returns the domain of the logged on user, which can be different from the machine

Update One

i’ve also tried using ADs object to bind to the IADs interface of the domain:

Читайте также:  Windows 10 терминал патч

problem with this approach is that:

  • you cannot get the domain name (only the distinguished name)
  • it doesn’t work if the user does not have permissions to query AD
  • it doesn’t work if the user is not a valid user in active directory
  • it only works for Active Directory domains

Update Two:

Just to be clear what i want is:

Bonus Reading

3 Answers 3

Anybody using DsRoleGetPrimaryDomainInformation in production use should consider calling DsRoleFreeMemory to free the memory block when the information is no longer needed (as per discussion in the comments).

  • Domain Forest Name: e.g. stackoverflow.com
  • Domain DNS Name: e.g. stackoverflow.com
  • Domain NetBIOS Name: e.g. STACKOVERFLOW

If the machine is not joined to a domain, then both Forest and dns are blank, with only NetBios name filled with the name of the workgroup, e.g.:

  • Domain Forest Name: null
  • Domain DNS Name: e.g. null
  • Domain NetBIOS Name: e.g. WORKGROUP

The function also returns a flag indicating if the machine is joined to a domain:

  • DsRole_RoleMemberWorkstation : workstation that is a member of a domain
  • DsRole_RoleMemberServer : server that is a member of a domain
  • DsRole_RolePrimaryDomainController : primary domain controller
  • DsRole_RoleBackupDomainController : backup domain controller
  • DsRole_RoleStandaloneWorkstation : workstation that is not a member of a domain
  • DsRole_RoleStandaloneServer : server that is not a member of a domain

Can you get a Windows (AD) username in PHP?

I have a PHP web application on an intranet that can extract the IP and host name of the current user on that page, but I was wondering if there is a way to get/extract their Active Directory/Windows username as well. Is this possible?

16 Answers 16

Check the AUTH_USER request variable. This will be empty if your web app allows anonymous access, but if your server’s using basic or Windows integrated authentication, it will contain the username of the authenticated user.

In an Active Directory domain, if your clients are running Internet Explorer and your web server/filesystem permissions are configured properly, IE will silently submit their domain credentials to your server and AUTH_USER will be MYDOMAIN\user.name without the users having to explicitly log in to your web app.

I’ve got php mysql running on IIS — I can use $_SERVER[«AUTH_USER»] if I turn on Windows Authentication in IIS -> Authentication and turn off Anonymous authentication (important)

I’ve used this to get my user and domain:

$user will return a value like: DOMAIN\username on our network, and then it’s just a case of removing the DOMAIN\ from the string.

This has worked in IE, FF, Chrome, Safari (tested) so far.

Look at the PHP LDAP library functions: http://us.php.net/ldap.

Active Directory [mostly] conforms to the LDAP standard.

We have multiple domains in our environment so I use preg_replace with regex to get just the username without DOMAIN\ .

If you’re using Apache on Windows, you can install the mod_auth_sspi from

Instructions are in the INSTALL file, and there is a whoami.php example. (It’s just a case of copying the mod_auth_sspi.so file into a folder and adding a line into httpd.conf.)

Once it’s installed and the necessary settings are made in httpd.conf to protect the directories you wish, PHP will populate the $_SERVER[‘REMOTE_USER’] with the user and domain (‘USER\DOMAIN’) of the authenticated user in IE — or prompt and authenticate in Firefox before passing it in.

Info is session-based, so single(ish) signon is possible even in Firefox.

Оцените статью