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 Exchange
Discussions

Username Environment Variable Missing
The only way to be sure is: [System. Security.Principal.WindowsIdentity]::GetCurrent().Name More on reddit.com
🌐 r/PowerShell
12
5
February 1, 2021
What is the windows variable for a users full name, not just the username? - Programming & Development - Spiceworks Community
I’m running some tests on a batch file, and I know %username% gets me the username of the person logged in. But what I’m looking for is a % variable that will give me the full First and last name? Is there such a variable? More on community.spiceworks.com
🌐 community.spiceworks.com
4
May 13, 2015
Environment Variable for a users full name. | Windows
I was wondering if there was a variable for a users full name? I want to add a users full name into the reg key for the office user at login. I've been playing and I've used GP Preferences to replace the current value of CU\Software\Microsoft\Office\Common\UserInfo\UserName with %USERNAME% This w... More on edugeek.net
🌐 edugeek.net
May 16, 2010
batch file - How do you find the current user in a Windows environment? - Stack Overflow
For example /mnt/c/Windows/System32/whoami.exe works in WSL. 2024-10-29T20:16:55.28Z+00:00 ... It should be in %USERNAME%. Obviously this can be easily spoofed, so don't rely on it for security. Useful tip: type set in a command prompt will list all environment variables. More on stackoverflow.com
🌐 stackoverflow.com
🌐
Microsoft Learn
learn.microsoft.com › en-us › troubleshoot › windows-client › ue-v › percent-username-percent-unavailable
Variable %username% is unavailable - Windows Client | Microsoft Learn
If you deployed UE-V by using the %username% variable as part of the "Settings Storage" setting, the literal string will be used. So all user accounts write to the same folder.
🌐
Reddit
reddit.com › r/powershell › username environment variable missing
r/PowerShell on Reddit: Username Environment Variable Missing
February 1, 2021 -

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)

🌐
Spiceworks
community.spiceworks.com › programming & development
What is the windows variable for a users full name, not just the username? - Programming & Development - Spiceworks Community
May 13, 2015 - I’m running some tests on a batch file, and I know %username% gets me the username of the person logged in. But what I’m looking for is a % variable that will give me the full First and last name? Is there such a variabl…
🌐
Seven Forums
sevenforums.com › software › 119189-user-variable-username.html
User Variable %username% - Windows 7 Help Forums
Windows 7 Ultimate x32 · New 20 Oct 2010 #1 · Hi Guys and Girls, I wonder if someone might be able to help me. We use a product called wildfire 5 with user variables set as follows WT_PTC_ROOT = C:\PTC\Local_Data\%USERNAME% WT_PTC_HOME = C:\PTC\Local_Data\%USERNAME% This seems to work on first look at the set command in dos.
🌐
Windows 10 Forums
tenforums.com › tutorials › 121855-edit-user-system-environment-variables-windows.html
Edit User and System Environment Variables in Windows - Windows 10 Help Forums
November 17, 2018 - The variable (ex: "%UserProfile%") ... "C:\Users\<username>"). There are two types of environment variables: user environment variables (set only for current user) and system environment variables (set for all users). This tutorial will show you how to edit user and system environment variables in Windows 7, Windows ...
Find elsewhere
🌐
EduGeek
edugeek.net › forums › technical › windows
Environment Variable for a users full name. | Windows
May 16, 2010 - I was wondering if there was a variable for a users full name? I want to add a users full name into the reg key for the office user at login. I've been playing and I've used GP Preferences to replace the current value of CU\Software\Microsoft\Office\Common\UserInfo\UserName with %USERNAME% This w...
🌐
Rene Nyffenegger
renenyffenegger.ch › notes › Windows › dirs › Users › username › index
%USERPROFILE%
The directory pointed at with the environment variable %userprofile% stores personal data of a specific user. This value is alse found in the registry in the value of ProfileImagePath under the registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\S-1-5-SID. The value of %userprofile% usually is c:\Users\username (username being lowercase of %username%?)
🌐
SS64
ss64.com › nt › syntax-variables.html
Windows Environment Variables - Windows CMD
Variables have a percent sign on both sides: %ThisIsAVariable% The variable name can include spaces, punctuation and mixed case: %_Another Ex.ample% (This is unlike Parameter variables which only have one % sign and are always one character long: %A ) ... %AppData% — C:\Users\{username}\AppData\Roaming %AppData%\Microsoft\Windows\Start Menu\ ― Windows Start menu shortcuts %LocalAppData% ― C:\Users\{username}\AppData\Local %Temp% ― C:\Users\{Username}\AppData\Local\Temp %SystemRoot% ― C:\Windows %AllUsersProfile% ― C:\ProgramData %UserProfile% ― C:\Users\{username}
🌐
Rapidee
rapidee.com › en › environment-variables
Environment Variables - Rapid Environment Editor
Rapid Environment Editor - freeware Windows environment variables editor
🌐
Spiceworks
community.spiceworks.com › software & applications
Creating a Windows Shortcut with %username% Variable - Software & Applications - Spiceworks Community
February 4, 2014 - What I am trying to do is to create a Windows desktop shortcut to that user’s folder on one of our servers using the %username% variable. If it is created and added to the “Default” profile, then regardless of who logs into the computer will have access to their folder on the server.
🌐
Microsoft Learn
learn.microsoft.com › en-us › windows › deployment › usmt › usmt-recognized-environment-variables
Recognized environment variables | Microsoft Learn
When the XML files MigDocs.xml, MigApp.xml, and MigUser.xml are used, the environment variables can be used to identify folders that can be different on different computers. Constant special item ID list (CSIDL) values provide a way to identify folders that applications use frequently but could have different names or locations on any given computer. For example, the Documents folder could be C:\Users\<Username>\Documents on one computer and C:\Users\<Username>\My Documents on another.
Top answer
1 of 3
1

Here is another batch file solution with code similar to code written by Martin Prikryl with three enhancements.

  1. %USERPROFILE% is used instead of C:\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 drive C: or in a different directory than C:\Users which is of course possible too.

  2. %SystemRoot%\System32\ftp.exe is used in the batch file instead of just ftp to make this batch file work also if by chance there is an ftp.* file with a file extension listed in environment variable PATHEXT in current directory or any other directory in environment variable PATH and not being the ftp executable in Windows system directory.

  3. 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
2 of 3
1

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