Determining DNS Domain

Posted on 2005-05-12
Last Modified: 2012-06-27
How do I get the DNS Domain of the local PC with a VB Script?

For example, if my host is bob and my domain is, how do I get the

I need this on a Windows 2000 PC...
Question by:rkogelhe
    LVL 13

    Assisted Solution


    Try this...

    Dim WshNetwork

    Set WshNetwork = WScript.CreateObject("WScript.Network")

    MsgBox WshNetwork.UserDomain
    LVL 12

    Assisted Solution

    That should prob read:

    Dim WshNetwork As Object

    Set WshNetwork = CreateObject("WScript.Network")

    MsgBox WshNetwork.UserDomain

    ; - )
    LVL 3

    Author Comment


    That seems to just display the first part of the domain.

    It displays just "foo". Not "". Note that "foo" is the same as the NT Domain as well, so I wonder if this script displays the NT Domain instead of the DNS Domain.
    LVL 70

    Accepted Solution


    A bit late in the posting... but if you still need it...

    If you have permission to access WMI this can get the domain name on a 200x machine:

    Const wbemFlagReturnImmediately = &h10
    Const wbemFlagForwardOnly = &h20

    strComputer = "."

    Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
    Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", _
                                              wbemFlagReturnImmediately + wbemFlagForwardOnly)

    For Each objItem in colItems
          wscript.echo objItem.Domain
    LVL 3

    Author Comment

    Thanks Chris, but that also seems to return just "foo", not ""...

    I found a way around the problem for now. But it would still be nice to know how to do this. What we need is the "gethostbyname" that you find in Unix...
    LVL 3

    Author Comment

    You'd figure that this should be in the MicrosoftDNS WMI, but I guess that wouldn't work if your DNS was provided by some other service.

    After reading a bit on this, I think that this needs to do a reverse DNS lookup of the name using the IP address, then parse the name for the domain.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
    Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now