- How is it possible to get Windows default user profile path via CMD?
- 3 Answers 3
- How do you find the current user in a Windows environment?
- 15 Answers 15
- PowerShell
- Not the answer you’re looking for? Browse other questions tagged windows batch-file or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Getting user temporary folder path in Windows
- 9 Answers 9
- Windows shell command to get the full path to the current directory?
- 14 Answers 14
- What is the alternative for
- 11 Answers 11
How is it possible to get Windows default user profile path via CMD?
Windows default user profile is usually %SystemDrive%\users\default but its real location is not an environment variable but it can be useful to have it. Default user profile path can be detected by system registry query like that
But we get the value %SystemDrive%\Users\Default where %SystemDrive% is not converted to C: (or other letter) so environment variable %defaultuserprofile% can not be used as normal environment variable path for file operations. Alternative variant can be something like
But it seems to be not so good. So how is it possible to get Windows default user profile path via CMD?
3 Answers 3
Don’t your really want %PUBLIC% , which is C:\Users\Public ? At least that’s closest equivalent on Windows 10 to a «Default» folder.
The type of the data is REG_EXPAND_SZ . To expand the %SystemDrive% embedded in the string, use call set . call causes another parse of the command so that set assigns C:\ instead of %SystemDrive% .
Like the 2nd OP code, which is not as reliable as the reg query:
If you want to view the Default folder and other hidden folders in the Users directory, use:
Output is the symlink named All Users , the folder named Default and the symlink named Default User on Windows 10 v1903.
Firstly, just to mention the purpose of the Default user profile is as the basis for all new user profiles. Therefore it should be used only to configure applications, settings and customizations for each future new user account on the PC. (modifications within it do not propagate to existing users).
The following method is similar to the existing answer, in that an additional expansion is used to cater for any variables within the contained data string. Please note that the value type of the data is by default REG_EXPAND_SZ, but it can also be of type REG_SZ, (the expand type is only necessary if the string contains a variable).
I have Rem arked a line with comment, which can be optionally omitted, and provided an example line below it for demonstration purposes, please modify that as necessary.
The code uses full paths for all external commands, (via a special variable, %__APPDIR__% , which will always point to their correct location). I have done that in order to remove any reliance on the content of the editable environment variables %PATH% and %PATHEXT% , (deliberate, or accidental, modification of those two environment variables could otherwise break the code).
How do you find the current user in a Windows environment?
When running a command-line script, is it possible to get the name of the current user?
15 Answers 15
You can use the username variable: %USERNAME%
Username:
Domainname:
You can get a complete list of environment variables by running the command set from the command prompt.
Just use this command in command prompt
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.
%USERNAME% is the correct answer in batch and other in Windows environments.
Another option is to use %USERPROFILE% to get the user’s path, like C:\Users\username .
The answer depends on which «command-line script» language you are in.
In the old cmd.exe command prompt or in a .bat or .cmd script, you can use the following:
%USERNAME% — Gets just the username.
%USERDOMAIN% — Gets the user’s domain.
PowerShell
In the PowerShell command prompt or a .ps1 or .psm1 script, you can use the following:
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name — Gives you the fully qualified username (e.g. Domain\Username). This is also the most secure method because it cannot be overridden by the user like the other $Env variables below.
$Env:Username — Gets just the username.
$Env:UserDomain — Gets the user’s domain.
$Env:ComputerName — Gets the name of the computer.
%USERNAME% will get you the username of the currently running process. Depending on how you are running your batch file, this is not necessarily the same as the name of the current user. For example, you might be running your batch file through a scheduled task, from a service, etc.
Here is a more sure way of getting the username of the currently logged on user by scraping the name of the user that started the explorer.exe task:
I use this method in writing batch files for testing.
Since you must include the password in plain text if authentication is required, I will only use it in a completely private environment where other users cannot view it or if a user seeing the password would bear no consequences.
Hope this helps someone out.
It’s always annoyed me how Windows doesn’t have some of more useful little scripting utilities of Unix, such as who/whoami, sed and AWK. Anyway, if you want something foolproof, get Visual Studio Express and compile the following:
And just use that in your batch file.
In most cases, the %USERNAME% variable will be what you want.
However, if you’re running an elevated cmd shell, then %USERNAME% will report the administrator name instead of your own user name. If you want to know the latter, run:
Just type whoami in command prompt and you’ll get the current username.
This is the main difference between username variable and whoami command:
In a standard context, each connected user holds an explorer.exe process: The command [tasklist /V|find «explorer»] returns a line that contains the explorer.exe process owner’s, with an adapted regex it is possible to obtain the required value. This also runs perfectly under Windows 7.
In rare cases explorer.exe is replaced by another program, the find filter can be adapted to match this case. If the command return an empty line then it is likely that no user is logged on. With Windows 7 it is also possible to run [query session|find «>»].
As far as find BlueBearr response the best (while I,m running my batch script with eg. SYSTEM rights) I have to add something to it. Because in my Windows language version (Polish) line that is to be catched by «%%a %%b»==»User Name:» gets REALLY COMPLICATED (it contains some diacritic characters in my language) I skip first 7 lines and operate on the 8th.
Via powershell (file.ps1) I use the following
It returns the name of the user in the «Domain\Username» format. If you just want the username just write
The advantage is that It works with windows 10 windows 8 server 2016. As far as I remember with also other OS like Win7 etc. (not older) . And yeah via batch you can simply use
Not the answer you’re looking for? Browse other questions tagged windows batch-file or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Getting user temporary folder path in Windows
How I can get the user’s temp folder path in C++? My program has to run on Windows Vista and XP and they have different temp paths. How I can get it without losing compatibility?
9 Answers 9
Is there a reason you can’t use the Win32 GetTempPath API?
This API is available starting with W2K and hence will be available on all of your listed targets.
Since C++ 17 you can use a cross-platform function: std::filesystem::temp_directory_path()
The GetTempPath function retrieves the path of the directory designated for temporary files. This function supersedes the GetTempDrive function.
Parameters
Specifies the size, in characters, of the string buffer identified by lpBuffer.
Points to a string buffer that receives the null-terminated string specifying the temporary file path.
Return Values
If the function succeeds, the return value is the length, in characters, of the string copied to lpBuffer, not including the terminating null character. If the return value is greater than nBufferLength, the return value is the size of the buffer required to hold the path. If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
The GetTempPath function gets the temporary file path as follows:
- The path specified by the TMP environment variable.
- The path specified by the TEMP environment variable, if TMP is not defined.
- The current directory, if both TMP and TEMP are not defined.
Windows shell command to get the full path to the current directory?
Is there a Windows command line command that I can use to get the full path to the current working directory?
Also, how can I store this path inside a variable used in a batch file?
14 Answers 14
Use cd with no arguments if you’re using the shell directly, or %cd% if you want to use it in a batch file (it behaves like an environment variable).
You can set a batch/environment variable as follows:
sample screenshot from a Windows 7 x64 cmd.exe.
Update: if you do a SET var = %cd% instead of SET var=%cd% , below is what happens. Thanks to jeb.
Quote the Windows help for the set command ( set /? ):
Note the %CD% — expands to the current directory string. part.
This has always worked for me:
For Windows we can use
command is there.
For Windows, cd by itself will show you the current working directory.
For UNIX and workalike systems, pwd will perform the same task. You can also use the $PWD shell variable under some shells. I am not sure if Windows supports getting the current working directory via a shell variable or not.
On Windows:
CHDIR Displays the name of or changes the current directory.
In Linux:
PWD Displays the name of current directory.
Based on the follow up question (store the data in a variable) in the comments to the chdir post I’m betting he wants to store the current path to restore it after changeing directories.
The original user should look at «pushd», which changes directory and pushes the current one onto a stack that can be restored with a «popd». On any modern Windows cmd shell that is the way to go when making batch files.
If you really need to grab the current path then modern cmd shells also have a %CD% variable that you can easily stuff away in another variable for reference.
What is the alternative for
I’m trying to use the command prompt to move some files, I am used to the linux terminal where I use
to specify the my home directory I’ve looked everywhere but I couldn’t seem to find it for windows command prompt ( Documents and Settings\[user] )
11 Answers 11
You’re going to be disappointed: %userprofile%
You can use other terminals, though. Powershell, which I believe you can get on XP and later (and comes preinstalled with Win7), allows you to use
for home directory.
You can %HOMEDRIVE%%HOMEPATH% for the drive + \docs settings\username or \users\username .
You can use %systemdrive%%homepath% environment variable to accomplish this.
The two command variables when concatenated gives you the desired user’s home directory path as below:
Running echo %systemdrive% on command prompt gives:
Running echo %homepath% on command prompt gives:
When used together it becomes:
Update — better version 18th July 2019.
Final summary, even though I’ve moved on to powershell for most windows console work anyway, but I decided to wrap this old cmd issue up, I had to get on a cmd console today, and the lack of this feature really struck me. This one finally works with spaces as well, where my previous answer would fail.
In addition, this one now is also able to use
as a prefix for other home sub-folders too, and it swaps forward-slashes to back-slashes as well. So here it is;
Step 1. Create these doskey macros, somewhere they get picked up every time cmd starts up.
Step 2. Create the cdtilde.bat file and put it somewhere in your PATH
Tested fine with;
Oh, also it allows lazy quoting, which I found useful, even when spaces are in the folder path names, since it wraps all of the arguments as if it was one long string. Which means just an initial quote also works, or completely without quotes also works.
All other stuff below may be ignored now, it is left for historical reasons — so I dont make the same mistakes again
old update 19th Oct 2018.
In case anyone else tried my approach, my original answer below didn’t handle spaces, eg, the following failed.
I think there must be a way to solve that. Will post again if I can improve my answer. (see above, I finally got it all working the way I wanted it to.)
My Original Answer, still needed work. 7th Oct 2018.
I was just trying to do it today, and I think I got it, this is what I think works well;
First, some doskey macros;