locked
How to filter the output and redirect as a variable RRS feed

  • Question

  • Hi all! I am trying to create a simple batch script that finds all .doc documents, which are located at the Documents folder of the currently logged in user. I have tried to catch the currently logged in user by typing:

    wmic computersystem get username /FORMAT:list

    and the output is:

    UserName=domain_name\user_name.

    Does anybody know how can I get just user_name instead of domain_name\user_name and to redirect it as a %variable% and after that the value of the variable to be put in the following command:

    cd  /d  "C:\Users\%variable%\Documents"

    Thank you all in advance!

    Thursday, June 18, 2015 3:31 PM

Answers

  • So just add a new line to split the username string:


    @echo off
    setlocal enableextensions
    for /f "tokens=1,2 delims==" %%A in ('%SystemRoot%\system32\Wbem\wmic.exe ComputerSystem GET UserName /VALUE ^| %SystemRoot%\system32\find.exe "UserName="') do set LOGGEDONUSER=%%B
    for /f "tokens=1,2 delims=\" %%A in ('echo %LOGGEDONUSER%') do set LOGGEDONUSER=%%B
    echo Logged on user: %LOGGEDONUSER%
    endlocal
    

    Note that PowerShell is still recommended:


    (get-wmiobject Win32_ComputerSystem).UserName.Split("\")[1]


    -- Bill Stewart [Bill_Stewart]


    • Proposed as answer by Bill_Stewart Thursday, June 18, 2015 5:39 PM
    • Edited by Bill_Stewart Thursday, June 18, 2015 5:57 PM
    • Marked as answer by Bill_Stewart Tuesday, July 21, 2015 9:05 PM
    Thursday, June 18, 2015 5:24 PM

All replies

  • Why WMIC? Just use the %USERNAME% environment variable. (PowerShell syntax: $Env:USERNAME).

    -- Bill Stewart [Bill_Stewart]

    Thursday, June 18, 2015 3:54 PM
  • I forgot to mention that this script will be pushed to all workstations in the domain through SCCM. I already tried to use %username%, however the script returns NT Authority user (the user account under which the SCCM executes the script). That's why I want to gather the username of the currently logged user.
    Thursday, June 18, 2015 3:59 PM
  • Cmd.exe shell script ugliness:


    @echo off
    setlocal enableextensions
    for /f "tokens=1,2 delims==" %%A in ('%SystemRoot%\system32\Wbem\wmic.exe ComputerSystem GET UserName /VALUE ^| %SystemRoot%\system32\find.exe "UserName="') do set LOGGEDONUSER=%%B
    echo Logged on user: %LOGGEDONUSER%
    endlocal
    

    PowerShell is much simpler:


    $loggedOnUser = (get-wmiobject Win32_ComputerSystem).UserName
    


    -- Bill Stewart [Bill_Stewart]

    Thursday, June 18, 2015 4:07 PM
  • Thanks Bill, however the script returns the username as "domain\username" instead of "username" only. Could you please post a script that returns just the username without domain\? I will be very appreciate.

    Regards,

    Tony

    Thursday, June 18, 2015 4:56 PM
  • What's wrong with having the domain name?

    -- Bill Stewart [Bill_Stewart]

    Thursday, June 18, 2015 5:00 PM
  • I want to use  %loggedonuser% here:

    cd  /d  "c:\users\%loggedonuser%\documents"

    With domain\username it won't work.

    Regards,

    Tony

    Thursday, June 18, 2015 5:16 PM
  • So just add a new line to split the username string:


    @echo off
    setlocal enableextensions
    for /f "tokens=1,2 delims==" %%A in ('%SystemRoot%\system32\Wbem\wmic.exe ComputerSystem GET UserName /VALUE ^| %SystemRoot%\system32\find.exe "UserName="') do set LOGGEDONUSER=%%B
    for /f "tokens=1,2 delims=\" %%A in ('echo %LOGGEDONUSER%') do set LOGGEDONUSER=%%B
    echo Logged on user: %LOGGEDONUSER%
    endlocal
    

    Note that PowerShell is still recommended:


    (get-wmiobject Win32_ComputerSystem).UserName.Split("\")[1]


    -- Bill Stewart [Bill_Stewart]


    • Proposed as answer by Bill_Stewart Thursday, June 18, 2015 5:39 PM
    • Edited by Bill_Stewart Thursday, June 18, 2015 5:57 PM
    • Marked as answer by Bill_Stewart Tuesday, July 21, 2015 9:05 PM
    Thursday, June 18, 2015 5:24 PM
  • It works!!! Many thanks, Bill.

    Thursday, June 18, 2015 5:38 PM