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

Jerold Schulman

December 18, 2000

1 Min Read
ITPro Today logo in a gray background | ITPro Today

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), Jerry@jsiinc 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?



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like