I need a similar command to Linux' domainname on Windows without using any third-party application.
Is there such a command?
I need a similar command to Linux' domainname on Windows without using any third-party application.
Is there such a command?
Try:
echo %USERDOMAIN% or
echo %USERDNSDOMAIN% If that still doesn't work, you can try using systeminfo:
systeminfo | findstr /B /C:"Domain" You can run below mentioned command on command prompt to get the desired information:
set user It gives us lot more information in addition to domain name (Refer screenshot):
List of details returned by the command are as below:
Important Note: Domain in which your computer is registered might not be the same as the one in which the logged-in user is registered. Reader is encouraged to explore - transitivity and direction in domain trust. This will help you to know about how a user registered in one domain can login to a computer registered in a different domain.
gci env:USER* is the PowerShell equivalent - handy when cmd.exe is blocked. Though in strange order for whatever reason: on one PC I get USERDNSDOMAIN USERPROFILE USERNAME USERDOMAIN, on another one USERPROFILE USERNAME USERDOMAIN_ROAMINGPROFILE USERDOMAIN USERDNSDOMAIN. See also here, SO and serverfault I just want to add my own solution just in case someone needs a fast way to do it.
My approach is using registry to get domain info:
reg query "HKLM\System\CurrentControlSet\Services\Tcpip\Parameters" /v "Domain" The %USERDOMAIN% and the network computer domain can be different. The systeminfo command will get the right answer but it is SLOW! Here is a solution I've used:
@REM + find the computer domain name FOR /F "usebackq tokens=*" %%a IN (`ipconfig /all`) DO ( @((ECHO %%a | findstr /i /c:"Primary Dns Suffix") && SET _str=%%a) > NUL 2>&1 ) FOR /F "tokens=2 delims=:" %%a IN ("%_str%") do SET _computerDomain=%%a SET _computerDomain=%_computerDomain: =% SET _fqdn=%COMPUTERNAME%.%_computerDomain% @Mike: fine solution - but I had some problems with it in a multi-language enviroment. I have German and English servers.
I changed your script to use wmic.exe:
@REM + Find the computer domain name @echo off FOR /F "usebackq tokens=*" %%a IN (`wmic.exe COMPUTERSYSTEM GET DOMAIN /Value`) DO ( @((ECHO %%a | findstr /i /c:"Domain=") && SET _str=%%a) > NUL 2>&1 ) FOR /F "tokens=2 delims=^=" %%a IN ("%_str%") do SET _computerDomain=%%a SET _computerDomain=%_computerDomain: =% SET _fqdn=%COMPUTERNAME%.%_computerDomain% echo %_fqdn% Thx for your idea
One line is enough to get the domain using a local user:
FOR /F "usebackq tokens=2 delims==" %%a IN (`wmic.exe COMPUTERSYSTEM GET DOMAIN /Value ^|find /i "domain"`) DO set _computerDom=%%a Most of the answers here aren't quite right. The settings mentioned are for the configured domain, however that may not be the domain the computer is actually on. The domain changes depending on what the computer is connected to - one WiFi connection may go to one domain, and a different one to another. Then connecting on VPN will put you in yet another domain. In all the cases the registry setting shown, and the USERDOMAIN and USERDNSDOMAIN environment variable values will remain unchanged.
To get the actual domain on which the computer is connected - use the "ipconfig" command line command, and see the "Connection-specific DNS Suffix" value. Note, for instance that if you disconnect or disable your connection, that will go away, indicating (correctly) that you are no longer in any domain.
A computer can, in fact, be connected on multiple domains, even, by connecting multiple ethernet ports, wifi ports, etc. to different places.
Just to throw another into the pot, for an ActiveDirectory domain-joined machine (not user), using ADSI (via .NET):
[System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain().name This may not be the same as the DNS domain the machine is using.
Tip of the hat to LeeM for his input on Active Directory vs. DNS domain names and registry locations.
Preface:
The Linux domainname utility reports the NIS/YP domain name, though a dnsdomainname utility for reporting the DNS one also exists.
By analogy, the Windows equivalent to the platform-native NIS/YP domain name would be the AD (Active Directory) domain name, which, as LeeM notes, may or may not be the same as the DNS one.
The solutions below cover both.
To offer PowerShell solutions:
To obtain the AD domain name, use CIM (WMI):
# AD domain name only. (Get-CimInstance Win32_ComputerSystem).Domain # FQDN (fully qualified domain name, including hostname) ($cs = Get-CimInstance Win32_ComputerSystem).DNSHostName + '.' + $cs.Domain Note:
On non-domain-joined machines, the DNS domain is reported as WORKGROUP
Get-CimInstance calls are versatile, but comparatively slow; LeeM's own answer shows a faster .NET API alternative; note that it throws an exception (statement-terminating error) on non-domain machines.
To obtain the DNS domain name, using a .NET API, which also works on non-Windows platforms:
# DNS domain name only. [System.Net.Dns]::GetHostEntry('').HostName -replace '^[^.]+\.?' # FQDN (fully qualified domain name, including hostname) [System.Net.Dns]::GetHostEntry('').HostName Note:
On non-domain-joined machines, the DNS domain is effectively the empty string.
The DNS domain name (and hostname) is also stored in the Windows registry and can alternatively be retrieved as follows (also returns the empty string on non-domain machines):
# Note: 'gpv' is a built-in alias of 'Get-ItemPropertyValue' gpv HKLM:\System\CurrentControlSet\Services\Tcpip\Parameters Domain Get-ItemPropertyValue -Path 'HKLM:\System\CurrentControlSet\Services\Tcpip\Parameters' -Name 'Domain' Domain property in Win32_ComputerSystem is the Active Directory domain or workgroup that the machine is a member of. [System.Net.Dns] (and the reg key, which is still valid to query, although [System.Net.Dns] is "nicer") is listing the DNS domain of the machine. This may or may not be the same as the AD domain. The Linux domainname command actually lists the machine's DNS domain/zone (it's definitely possible the OP wants the AD domain). My answer didn't clarify that either, to be fair.