How do you get the User Profile name in a Batch File without the complete path?

0

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.

Kyle M

Posted 2015-03-05T12:26:24.687

Reputation: 3

I don't understand. You should be using %USERNAME% to get the path then split the string if you just want the username. – Ramhound – 2015-03-05T12:30:07.907

1Hmm. echo %USERNAME% just gives my name here on Windows 7 64bit. – DavidPostill – 2015-03-05T12:33:25.973

Ok, so I could split the %USERNAME% output then. That would work. I just wondered if there was an easier way to get the Username is all. – Kyle M – 2015-03-05T12:36:50.780

Yeah I noticed that echo %USERNAME% gives JUST the name so I was confused. – Kyle M – 2015-03-05T12:38:52.900

Answers

5

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

DavidPostill

Posted 2015-03-05T12:26:24.687

Reputation: 118 938