for /F "tokens=1 delims=" %%i in ('query user %userID% /server:ServerName') do set ans=%%i
If the query user outputs lines before the one that has the data you want, you need to add skip=n
where n is the number of lines of output before the one that is interesting to you. Specifying appropriate delimiters for the "delims="
will allow you to have it parsed.
Wouldn't PowerShell be a better way go though?
If the output contains more than one meaningful lines of output the above will only give you the results from the last line. Adding delayed variable substitution and compound commands after the "do" by enclosing them in () could allow you handle that.
Or you could redirect the output to a file query user %UserID% /user:ServerN >ServerN.txt and then parse that with a for /f ...