Hi guys.
ECHO "Testing 123"
FOR /F "tokens=1 skip=1" %%A IN ('WMIC COMPUTERSYSTEM GET username') DO IF NOT "%%A"=="" SET LOGGEDNAME=%%A
ECHO %LOGGEDNAME%
Can you help me with this ?
Can't get it to display %LOGGEDNAME%...
I think something about the part ---> DO IF NOT "%%A"==""
http://pastebin.com/Z7SKDMWK
Thanks.
Need help with piping wmic output to batch file variable
Moderator: DosItHelp
Re: Need help with piping wmic output to batch file variable
Hi, It works with me here fine and display the "Computername\Username"
maybe you have spaces in you user name or computer name, try putting double quotes around like this "LOGGEDNAME=%%A"
maybe you have spaces in you user name or computer name, try putting double quotes around like this "LOGGEDNAME=%%A"
Re: Need help with piping wmic output to batch file variable
This gets the info, without trailing spaces:
If you just want the username then %username% is available too.
This suffers from trailing spaces, the WMIC scourge.
This also gets the info but with trailing spaces.
If you just want the username then %username% is available too.
Code: Select all
@echo off
ECHO "Testing 123"
FOR /F "skip=2 tokens=1,* delims=," %%A IN ('WMIC COMPUTERSYSTEM GET username /format:csv') do SET LOGGEDNAME=%%B
ECHO "%LOGGEDNAME%"
echo "%username%"
pause
This suffers from trailing spaces, the WMIC scourge.
Code: Select all
@echo off
ECHO "Testing 123"
FOR /F "delims=" %%A IN ('WMIC COMPUTERSYSTEM GET username ^|find "\"') DO SET LOGGEDNAME=%%A
ECHO "%LOGGEDNAME%"
echo "%username%"
pause
This also gets the info but with trailing spaces.
Code: Select all
@echo off
ECHO "Testing 123"
FOR /F "skip=1 delims=" %%A IN ('WMIC COMPUTERSYSTEM GET username') do if not defined loggedname SET LOGGEDNAME=%%A
ECHO "%LOGGEDNAME%"
echo "%username%"
pause
Re: Need help with piping wmic output to batch file variable
Much thanks foxidrive.
That'd help me with other wmic query too.
I tried %USERNAME% but it seems the env var is populated even if a service remotely logged in as service.
WMIC computersystem get username only shows when someone really logged into the workstation/desktop.
Thanks again.
That'd help me with other wmic query too.
I tried %USERNAME% but it seems the env var is populated even if a service remotely logged in as service.
WMIC computersystem get username only shows when someone really logged into the workstation/desktop.
Thanks again.