I just rattled this together for a friend, could be useful.
the FOR statement MUST be on a single line to work.
The first one is a bit rubbish, it determines your session ID and if Zero tells you. However, if you have multiple identical usernames logged on this may not be of use, so see second code
@echo off
for /F "tokens=3 skip=1 delims= " %%a in ('query user %username%') Do if %%a==0 echo Session Zero In Use By %username% & goto finish
echo Session ID is NOT Zero!!
:finish
This should be more useful:
This actually looks at all sessions and when it finds the one for you (designated by the is greater than sign on query user output) it further checks to see if that is session Zero if it is it tells you, if it isn’t it tells you. Hopefully.
@echo off
for /F "tokens=1,3 skip=1 delims= " %%a in ('query user') do if /i %%a==^>%username% if %%b==0 echo "YOU ARE Using Session ZERO" & Goto finish
echo "You ARE NOT Using Session Zero"
:finish
A picture of the explanation is below. The /i in the “do if” says ignore case, so administrator & Administrator are the same.
The first token specified (token 1) is stored in the variable specified (%%A), the following tokens specified (in this case, token 3) is stored in variable %%B. Further tokens would be in %%C, %%D ….
Delims= Specifies the deliminator to use to seperate the tokens, the default is space(s)
See here for more info: http://ss64.com/nt/for_f.html