No announcement yet.

Batch script to set multiple user's password from text file

  • Filter
  • Time
  • Show
Clear All
new posts

  • Batch script to set multiple user's password from text file

    Hello, I'm hoping someone can help me out here.

    I need to rebuild a server and need to recreate the 400 local user accounts on the original server.
    These user accounts (and passwords) are documented in a spreadsheet.

    I was hoping to put the list of usernames in one file "users.txt" and the list of corresponding passwords in another file "passwords.txt", then come up with a script (with a loop?) to:

    1. Put the username from "users.txt" in one variable (i.e. userx)
    2. Put the password from "passwords.txt" in another variable (i.e. passwordx)
    3. Run command "net user userx passwordx" which in theory would set the password for that particular user.
    4. Go back to the start of the loop and read next line for next user/password, populate the variable with those new values and set the password for that next user.

    I'm new to scripting, but I asumed I could do this in a FOR loop.
    As a test, I have 5 usernames in "users.txt" and 5 corresponding passwords in "passwords.txt"

    To check that the correct values were going to be passed to the variables, I first wrote an ECHO to display the values from the variables.
    I tried the following:

    FOR /F "delims=" %%G IN (users.txt) DO SET userx=%%G
    FOR /F "delims=" %%H IN (passwords.txt) DO SET passwdx=%%H
    ECHO User is %userx%
    ECHO Password is %passwdx%

    However, this just displays the last line in each text file:

    Userx is user5
    Passwordx is password5

    I assumed that I need to somehow put the variable as part of the FOR loop and it was getting confused by referring to 2 separate text files.
    So, I tried putting both user and password in tab-separated columns in 1 text file "both.txt".
    I read that in theory I should use the tokens option and pipe the variable to another batch file, called within the original batch file.

    I tried the following code in batch file "run.cmd":

    FOR /F "tokens=1,2" %%G IN (both.txt) DO CALL set-pwd.cmd

    The text for "set-pwd.cmd" is as follows:

    SET userx=%%G
    SET passwdx=%%H

    ECHO Userx is %userx%
    ECHO Passwordx is %passwdx%

    However, running the "run.cmd" script, this returns the following:

    User is %G
    Password is %H

    Can anyone help here? Clearly I've got the syntax wrong, or haven't grasped the concept correctly.

    Ideally, if someone could provide some examples, or point me at a script which actually achieves this, I can't be the first person to want to do something like this!

    If anyone could offer any help or advice, it would be greatly appreciated.


  • #2
    Re: Batch script to set multiple user's password from text file

    It'd be easier to use VBScript, and run it on a machine that has Excel installed on it. Put your usernames and passwords in adjacent columns, and just have the script step through the file. We use such a scheme with a bit more bells-n-whistles to create new users in our domain. I pieced all this together by hunting thru . The reason for needing Excel is that the script calls Excel, opens the file we prepared, reads and makes changes as applicable, then saves and closes it. We run this from a management workstation which has Office on it, rather than running the script directly on a DC. I would think that creating users in the SAM on a workstation would be fairly similar.

    One thing that came up when doing this at the domain level--you have to create and update the new user object first, before you can set a password to it.
    MSCA (2003/XP), Security+, CCNA

    ** Remember: credit where credit is due, and reputation points as appropriate **


    • #3
      Re: Batch script to set multiple user's password from text file

      Hi Loz,

      I think the main issue you're having is a lack of understanding of variables. When I was learning (still am) batch scripts the variables were very confusing for me.

      The first script goes through and sets the %userx% variable to each user in the list, then sets the %passwordx% to each password in the list, and then echos the variables at the end of the script. So it shows the last ones set.

      The second script loops through your list and calls the other batch file but that batch has no idea what %G and %H are.

      You can play around with this:
      FOR /F "tokens=1,2 delims=," %%a IN (both.txt) DO CALL :Sub_setpwd %%a %%b
      GoTo EOF
      ECHO Userx is %1
      ECHO Passwordx is %2
      In the script it assumes that the text file separates the username and password with a comma. When I call the subroutine you see the %%a and %%b are specified as arguments to the called subroutine. When in the subroutine I can refer to the arguments in order as %1, %2, %3, etc.

      What OS will be running AD? If it's 2008 R2 then using PowerShell would be much easier and more robust than batch. Also, if you have special characters in the passwords then using VBS or PowerShell would be easier than building the logic into a batch script.

      Network Consultant/Engineer
      Baltimore - Washington area and beyond


      • #4
        Re: Batch script to set multiple user's password from text file

        Does the server has PowerShell installed? It can be done quite easily then.

        There is even a nice example in the TechNet galleries.
        Last edited by Dumber; 4th December 2012, 18:25.
        Technical Consultant

        MCITP(EA, SA), MCSA/E 2003:Security, CCNA, SNAF, DCUCI, CCSA/E/E+ (R60), VCP4/5, NCDA, NCIE - SAN, NCIE - BR, EMCPE
        "No matter how secure, there is always the human factor."

        "Enjoy life today, tomorrow may never come."
        "If you're going through hell, keep going. ~Winston Churchill"