0Day Forums
CMD.exe: Get second column output to variable - Printable Version

+- 0Day Forums (https://0day.red)
+-- Forum: Coding (https://0day.red/Forum-Coding)
+--- Forum: .bat & .wsf & .cmd (https://0day.red/Forum-bat-wsf-cmd)
+--- Thread: CMD.exe: Get second column output to variable (/Thread-CMD-exe-Get-second-column-output-to-variable)



CMD.exe: Get second column output to variable - pippyyr - 07-23-2023

I need the below command second column output `<USERNAME>` result to variables:

query sessions|find "Active"
>console <USERNAME> 1 Active

I know the `%USERNAME%` OR `whoami` could get the current user name but this script will run using administrator account and will need to be able to capture the current active logon username. If I could select the second column of the output results and assign it to a variable.. this will be a great help.


RE: CMD.exe: Get second column output to variable - lucienf - 07-23-2023

In this case you should use `FOR /F` to capture the output

for /F "tokens=1,2,3,4,5" %%A in ('"query session | find "Active""') DO (
echo %%A,%%B,%%C,%%D,%%E
)

It splits also the line at spaces and TABs, but this can be problematic if the username contains spaces.