How to get the User Profile name in a Batch File without the complete path

batchusername

I've seen that you can use %USERNAME% to get the whole path but I just want to get e.g John Doe for use in mapping a network drive to specific folders named after the user profile names?

e.g
NET USE h: \test\Citrixusers\%USERNAME%
but of course this gets the username as C:\Users\"Name"

So the end result would be like \test\Citrixusers\John Doe

Thanks in advance all.

Best Answer

How do you get the User Profile name in a batch file?

without the complete path

It appears you are confusing %USERNAME% and %USERPROFILE%.

  • %USERNAME% contains just the username.

  • %USERPROFILE% contains the profile path (which includes the username).


Standard (built-in) Environment Variables

Variable Default value in Windows 7/2008

...

USERNAME {username}

USERPROFILE %SystemDrive%\Users\{username}

Source environment variables - Environment variables are mainly used within batch files, they can be created, modified and deleted for a session using the SET command.


Further Reading

Related Question