How can I change the windows USERNAME environment variable string?
@ECHO OFF
SETLOCAL
PUSHD "%temp%"
COPY NUL "%username%" >NUL 2>NUL
FOR /f "delims=" %%a IN ('dir /L /b "%username%"') DO set "usernamelc=%%a"
del "%username%" >NUL 2>NUL
POPD
ECHO %USERNAME% --^> %usernamelc%
GOTO :EOF
Perhaps this may help. YMMV.
will
Updated on June 05, 2022Comments
-
will almost 2 years
I am looking for a solution for this after struggling for most of the day. In short my dilemma seems simple. An effective solution appears elusive based on the Google and Stackoverflow hits I've had on this one.
(EDIT)
I need to clarify the use-case. We may have 6 users needing to run certain scripts that use the "%USERNAME%
" string to login to case-sensitive environments, Linux, MySQL, etc. One user-name is mixed-case. In the example I used my name: "Will" for the scripts it must-be: "will" (all lower-case).- The general question though is how can the %USERNAME% environment variable value be change when it is mis-entered?
At present my Windows 7 Professional username is "will" but the environment variable has this value:
echo. User = %USERNAME% User = Will
I need it to come-out as:
User = will rem * All lower-case
To be clear ...
I want to change (source) value Windows sets the USERNAME environment variable when I login.
I thought it would be easy to find the setting or config option to change that string -- Until I tried. I think this must happen frequently judging from questions related to getting a USERNAME to work with cygwin, etc.
The question
Bothers me too, because that's a messy script to "fix" something basic in a login-environment.
I did a quick survey around the office as well. Some people have mixed case, one or two have lowercase. It seems arbitrary depending on what was typed-in when account was created or something.
Our admin guy looked up my account on the domain (no, not active directory) and is same as my login-screen, "will" (lowercase).
Finally I did a couple of experiments. I changed my username in the Users control panel
from: "will" to "xxwill"
Thinking that would surely update my USERNAME string. I logged out. I did it again and rebooted a second time. The result was extremely surprising (or maybe it shouldn't have been):
User = Will rem * NO change for the "xxwill" name-change!
Some further grist. I created an account "dodo" and account "Dogdo" and changed the usernames to "Dodo" and "dogdo" respectively.
User = dodo User = Dogdo rem * NO change for name-changes!
I see that the USERNAME can't be altered that way. I can't edit the system environment variable in the Advanced options. It may be in the profile. But can you edit a profile or even look inside it with just Administrator powers? I don't know yet; I'm not sure that's my preferred route either since it is hacking with a bit of "output" from some original setting, somewhere.
I hope someone can set me straight so I don't need that script-solution.
-
will about 8 yearsI want to NOT need to manipulate the value. I think that Windows should set USERNAME to the name I logged in with. Since it won't I need to change it explicitly. If it is possible.
-
Magoo about 8 yearsYou could directly set
username
rather thanusernamelc
if you want, but such a setting is then only valid for the currentcmd
instance. It is re-established for other instances on creation of each instance, but setting it as a user variable overrides the system-setting (in thatcmd
instance) -
will about 8 yearsI think there must be a misunderstanding. CREATE a username of "Fred" o your Windows PC. Then change the login user's name to (say): "Malcom". So now the username is "Malcom". When YOU login as Malcom -- and print
echo %USERNAME%
... The output shall be, "Fred" (forever thus). I wannaknow, how to modify the string that is loaded-in-to ... USERNAME during login. -
Magoo about 8 yearsI don't play around with user-accounts since my system is only used by me and I'm not in a corporate environment. Think about the consequences of changing your username. How would you handle files' ownership and access for instance - If you're "Superman" today and "Batman" tomorrow, how is the system supposed to track those changes for network, local, cloud or off-line devices? Compared to that nighmare, propagating directory-change-date/time from a deep directory through to root (which Windows doesn't do) would be a doddle... Login name not necessarily = account name appears true.