Search code examples
cmdfindstr

findstr split by space and show 3rd word


On Windows 2012 I am running net user localusername | findstr "logon"

which returns Last logon 9/8/2017 9:27:16 AM

I only want it to return the date which will be different on each account. How do I filter this to only return 9/8/2017?

This is what I did for Windows 2016 and it works great, but Windows 2012 does not support grep or cut.

net user "{{ item }}" | grep "Last logon" | cut -d' ' -f21

which returns 9/8/2017

update The following is working on the server thanks to @Mofi

for /F "tokens=3" %I in ('net user "test" 2^>nul ^| findstr "Last logon"') do @echo %I

Solution

  • update The following is working on the server thanks to @Mofi

    for /F "tokens=3" %I in ('net user "test" 2^>nul ^| findstr "Last logon"') do @echo %I