Skip navigation

JSI Tip 3161. %UserName% environment variable in a Windows 2000 domain may not be set as expected?

According to Microsoft, it is possible to log on to a Windows 2000 domain with a logon name that is not an exact match with the one that is stored in the Active Directory, as long as there is no ambiguity, and you use the correct password.

NOTE: I was unable to reproduce this behavior.

This can cause a problem in any logon scripts or other batch job that you run, if they depend on the accuracy of the %UserName% environment variable, since the %UserName% environment variable is set from the logon name that you typed, NOT the one stored in the Active Directory.

Here are two workarounds:

1. Use the UPN (User Principal Name), [email protected] or [email protected], to log on.

2. You can add the following line to each batch that requires %UserName%:

for /f "Tokens=2 Delims=\" %%i in ('whoami') do set UserName=%%i

NOTE: You will need to modify this command if your domain name or user name contains spaces.

NOTE: See When can the %UserName% environment variable be different than the user name in Active Directory?

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.