The environment variable USERNAME is defined in the registry key HKEY_CURRENT_USER\Volatile Environment.
Note however that as the keyname implies, the variables in this key are volatile, meaning that while the user can change them, they will not retain their new values and will be overwritten by the system with derived values (sort of like registry RAM). The username environment variable is one of the few variables that cannot be set, or even if they are, they will not “stick”, just like how setting a variable in a console session is lost when you open a new one.
It should update the variable with the new user name. Have you tried logging out and back in or rebooting the system?
Where/how did you change the user name, the User Accounts Control Panel applet? The User Accounts applet only changes the user’s name, not the actual username. Use lusrmgr.msc to modify the user name by clicking it, pressing F2 and changing it (then log out/in or reboot).
If that does not work, then search the registry for the old/original user name and change it there(s?). Check the following keys:
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\SpecialAccounts\UserList
Answer from Synetech on Stack ExchangeIf 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.
@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.
Username Environment Variable Missing
What is the windows variable for a users full name, not just the username? - Programming & Development - Spiceworks Community
Environment Variable for a users full name. | Windows
batch file - How do you find the current user in a Windows environment? - Stack Overflow
Videos
This is a new one for me. I was debugging a failing script and found on some of our computers the $env:username variable isn't registered. I sign into the computer and I can see USERNAME listed as a System Variable and I can open up Command Prompt and use the %Username% variable but in PowerShell, it comes back as empty.
Has anyone run across this one or know how to recreate it for all users?
I'm thinking something along these lines maybe?
[Environment]::SetEnvironmentVariable(Username, someamazingthinghere)
Here is another batch file solution with code similar to code written by Martin Prikryl with three enhancements.
%USERPROFILE%is used instead ofC:\Users\%username%which makes this batch file solution work also on Windows XP and on machines on which the user's profile directory is not on driveC:or in a different directory thanC:\Userswhich is of course possible too.%SystemRoot%\System32\ftp.exeis used in the batch file instead of justftpto make this batch file work also if by chance there is an ftp.* file with a file extension listed in environment variablePATHEXTin current directory or any other directory in environment variablePATHand not being theftpexecutable in Windows system directory.The ISO file name is renamed before upload with including a random decimal number between 0 and 32767 as asked for with a comment.
The command lines of enhanced batch file:
:RandomIsoName
set "RandomName=mini_%RANDOM%.iso"
if exist "%USERPROFILE%\Desktop\ISO's\%RandomName%" goto RandomIsoName
ren "%USERPROFILE%\Desktop\ISO's\mini.iso" "%RandomName%"
(
echo open hostname
echo username
echo password
echo cd \wwwhome\Logs\
echo put "%USERPROFILE%\Desktop\ISO's\%RandomName%"
echo bye
)>ftp.txt
%SystemRoot%\System32\ftp.exe -s:ftp.txt
You have to generate the ftp script using that variable:
echo open hostname>ftp.txt
echo username>>ftp.txt
echo password>>ftp.txt
echo cd \wwwhome\Logs\>>ftp.txt
echo put "C:\Users\%username%\Desktop\ISO's\mini.iso">>ftp.txt
echo bye>>ftp.txt
ftp -s:ftp.txt