locked
Use PowerShell to find Particular text from a TXT file RRS feed

  • Question

  • Hello,

    I am trying to create a script to pull/find the usernames from a Log Files.

    The log files is located in a .LOG   The log file looks like the following

    SERVER02 , COMPUTER209 , USERNAME1 , Wed 11/28/2018 ,  6:15:36.00 
    SERVER101 , COMPUTER022 , USERNAME23 , Wed 11/28/2018 ,  8:53:24.14 

    My goal is to get only the username for now.

    Example:

    USERNAME1

    USERNAME23 

    and move those usersnames to another TXT file

    I was wondering if someone can help me with that.


    Thursday, November 29, 2018 3:18 PM

Answers

  • You might try something like this

    Get-Content C:\YourLogFile.txt | foreach{
        $username = $_ -split "," 
        Write-Output $username[2]
    }



    • Edited by Fernando_dos_Santos Thursday, November 29, 2018 3:25 PM
    • Marked as answer by mcubac Thursday, November 29, 2018 3:26 PM
    • Unmarked as answer by mcubac Thursday, November 29, 2018 4:09 PM
    • Marked as answer by mcubac Thursday, November 29, 2018 6:48 PM
    Thursday, November 29, 2018 3:24 PM

All replies

  • You might try something like this

    Get-Content C:\YourLogFile.txt | foreach{
        $username = $_ -split "," 
        Write-Output $username[2]
    }



    • Edited by Fernando_dos_Santos Thursday, November 29, 2018 3:25 PM
    • Marked as answer by mcubac Thursday, November 29, 2018 3:26 PM
    • Unmarked as answer by mcubac Thursday, November 29, 2018 4:09 PM
    • Marked as answer by mcubac Thursday, November 29, 2018 6:48 PM
    Thursday, November 29, 2018 3:24 PM
  • Thank you so much!!!
    Thursday, November 29, 2018 3:26 PM
  • Hmm, that could almost be a csv.

    import-csv file.log -Header server,computer,username,date,time |
    select -expand username

    USERNAME1
    USERNAME23

    • Edited by JS2010 Thursday, November 29, 2018 4:00 PM
    • Proposed as answer by ComputerScott Thursday, November 29, 2018 4:15 PM
    Thursday, November 29, 2018 3:59 PM
  • Hello Fernando,

    This works great, but I was wondering if you could help me make 1 change.

    Because the logs have lots of lines.. i just need the lines of the last 100 users (meaning, the ones from the bottom), how could I add that there?  

    Thank you so much!

    Get-Content C:\Test\Test.log | foreach{
        $username = $_ -split "," 
        $users = $username[2]
        Write-Output $users
      
    } | Out-File -Filepath C:\Test\Users.txt

    Thursday, November 29, 2018 4:19 PM
  • | select -last 100 | set-content c:\test\users.txt

    • Edited by JS2010 Thursday, November 29, 2018 4:22 PM
    Thursday, November 29, 2018 4:22 PM
  • Get-Content C:\temp\YourLogFile.txt | select -Last 100 | foreach{
        $username = $_ -split "," 
        Write-Output $username[2]
    }

    Thursday, November 29, 2018 4:34 PM