0

I'm trying to set the admin password through a .bat file using the command

net user Administrator my_password

if I'm writing that exact line in a command prompt everything works fine, I can login again after I have logged out or restarted. When I run the .bat file I'm unable to login after I've logged out or restart. What is the difference and how can I make it work from a .bat file or other script?

EDIT: I'm running the .bat file simply by clicking on it, I am logged in as administrator. The command does give a success message and it obviously changes the password since I can't use the old password afterwards.

Martin Riddar
  • 173
  • 4
  • 16

2 Answers2

1

I suppose the password string contains at least 1 non ASCII character, i.e. a character which has a code value greater decimal 127.

Open a command prompt window and enter chcp (change code page command). Depending on Windows region and language settings you get displayed that the active code page is code page 850 (Western European country) or code page 437 (North American country) or something different.

The batch file written in a Windows GUI text editor uses most likely a different code page like Windows-1252 (Western European and North American countries).

It is advisable to write a batch file with using same code page as used by Windows for console, or do not use non ASCII characters at all.

But account passwords are something very special. Windows knows that a password for an account is usually entered on GUI using the Windows GUI code page by a user. Therefore a password entered in a console window for an account is automatically converted from OEM code page to Windows (ANSI) code page before encrypting and saving it by Windows.

So the batch file should contain the password string encoded in OEM code page instead of the GUI code page.

For example German umlaut character ä has code value 228 (hex. E4) using Windows-1252, but has code value 132 (hexadecimal 84) using OEM code page 850. For printing into console window with command echo the German word ähnlich, it is necessary to write in batch file ä with code value 132 using OEM code page 850 to get this word correct displayed in console window. It is necessary to write character ä also with code value 132 into the batch file if this character is used in a password written into a batch file. The password string is automatically converted from OEM to ANSI by command net.

Mofi
  • 46,139
  • 17
  • 80
  • 143
1

I suppose your password contains a percent sign.

The difference is that percent signs at the cmd prompt are preserved.
But in a batch file the parser tries to expand a variable with percent sign, but when the percent expression isn't valid it will be removed.

If you want to use the password in a batch file you only have to double the percents.

More infos about variable expansion at How does CMD.EXE parse scripts?

Community
  • 1
  • 1
jeb
  • 78,592
  • 17
  • 171
  • 225