%username% variable in batch

Giles picture Giles · Jun 22, 2016 · Viewed 31.1k times · Source

I'd like to be able to pass the username variable to a cmd command in a batch script so I can see if the current user is an administrator. I tried this code start cmd.exe /c "net user %username% > priv.txt" It doesn't want to work though, whats the problem and is there a better way to do this? Any help or suggestions are appreciated. Thanks everyone.

Answer

Drako picture Drako · Jun 23, 2016

Why You don't just use that %username% variable in your .bat file? Just tested echo %username% returns current user name - seems no problem using it; if You have any exact case and reason why its not working - clarify case. if you want to output to file use

echo %username% >> file.txt
net user %username% >> file.txt