Batch file to get users name from a nt login name

Posted on 2008-11-06
Last Modified: 2012-05-05

There is a command you can use "net user" I belive which will able you to type in a username "tlewis" and it will give you the results to show your full username "terry lewis"

I need to have C:\batch\usernames.txt  and the batch file runs and exports to C:\batch\output.txt.

Does anyone have the script that does this.


Question by:ncomper
    LVL 67

    Accepted Solution

    How about a vbscript?
    Name it FindUsers.vbs or something similar...
    SourceFile = "C:\batch\usernames.txt"
    OutputFile = "C:\batch\output.txt"
    LDAPDomain = "LDAP://DC=Domain,DC=com"
    Dim objFSO : Set objFSO = CreateObject("Scripting.FileSystemObject")
    Dim objOut : Set objOut = objFSO.CreateTextFile(OutputFile)
    arrUsers = Split(objFSO.OpenTextFile(SourceFile).ReadAll, vbNewLine)
    Dim conn : Set conn = CreateObject("ADODB.Connection")
    Dim cmd : Set cmd = CreateObject("ADODB.Command")
    conn.Provider = "ADsDSOObject"
    conn.Open "Active Directory Provider"
    Set cmd.ActiveConnection = conn
    cmd.Properties("Page Size") = 1000
    cmd.Properties("SearchScope") = ADS_SCOPE_SUBTREE
    For Each UserName in arrUsers
      cmd.CommandText = "SELECT givenName, sn FROM '" & LDAPDomain & "' WHERE objectCategory = 'user' AND SAMAccountName = '" & UserName &  "'"
      Dim objRS : Set objRS = cmd.Execute
      Do Until objRS.EOF
        strFullName = objRS.Fields(0) & " " & objRS.Fields(1)
        objOut.WriteLine UserName & vbTab & strFullName

    Open in new window

    LVL 5

    Author Closing Comment


    Sorry it took a while to copme back

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    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.

    When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
    Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    759 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

    11 Experts available now in Live!

    Get 1:1 Help Now