4

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
  • 4,799
  • 8
  • 54
  • 90

3 Answers3

2

If you want to change the windows USERNAME environment variable string, you can do this in the Advanced User Accounts Control Panel.

Open it by running netplwiz ("C:\Windows\System32\Netplwiz.exe", network places wizard). Then select the name you want to change and click on the Properties button. Now you get the properties of the selected user account: you can change the User name (, Full name & Description) here. You'll (just) need administrator rights to do this.

Log out and log on, open a command prompt and try to output the value for %USERNAME%. You'll notice it has been changed according to your modification.

Stijn Bousard
  • 331
  • 2
  • 8
1
@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.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • 1
    I 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. – will Apr 08 '16 at 06:34
  • You could directly set `username` rather than `usernamelc` if you want, but such a setting is then only valid for the current `cmd` 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 that `cmd` instance) – Magoo Apr 08 '16 at 06:48
  • 1
    I 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. – will Apr 08 '16 at 14:25
  • I 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. – Magoo Apr 08 '16 at 14:50
0

I don't think there is a way to do exactly what you are saying, but it might be achievable, you will know by playing around with the locations where usernames are defined.

• Firstly, there is the user folder C:\user\username1 and that is defined at user creation. This can (I think) be changed (I've only seen references to it, but there are many references, this is easy to find).

• Secondly, there is the Control Panel > User Accounts > Change My Name. This seems to tie in with your $env:USERNAME

• Finally, there is the lusrmgr.msc > Users. This is tied most closely to your "real" username, and aligns with [Security.Principal.WindowsIdentity]::GetCurrent() I say "real" because I found this recently when trying to use ssh-keygen to create a public key and although my Display Name and $env:USERNAME were set to "Name1", ssh-keygen would only see "Name2" and I had to alter the name in lusrmgr.msc to fix this (a reboot was required).

This is an old post, but posting in case this can help anyone with similar issues.

YorSubs
  • 3,194
  • 7
  • 37
  • 60