Need help with piping wmic output to batch file variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
fuleo
Posts: 4
Joined: 17 Feb 2013 21:39

Need help with piping wmic output to batch file variable

#1 Post by fuleo » 17 Feb 2013 21:42

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.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: Need help with piping wmic output to batch file variable

#2 Post by abc0502 » 18 Feb 2013 03:45

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"

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Need help with piping wmic output to batch file variable

#3 Post by foxidrive » 18 Feb 2013 06:13

This gets the info, without 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

fuleo
Posts: 4
Joined: 17 Feb 2013 21:39

Re: Need help with piping wmic output to batch file variable

#4 Post by fuleo » 18 Feb 2013 21:07

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.

Post Reply