select-string NOT working

Welcome Forums General PowerShell Q&A select-string NOT working

This topic contains 1 reply, has 2 voices, and was last updated by

 
Participant
1 year, 11 months ago.

  • Author
    Posts
  • #57061

    Participant
    Points: 0
    Rank: Member

    I am trying to find the 1st occurrence of a string in each file. But it returns ALL the occurrences in each file. What am I doing wrong?
    Select-String -Path "c:\downloads\trash\*" -Pattern "aaa"

    C:\downloads\trash\1.txt:1:aaa
    C:\downloads\trash\1.txt:3:aaa
    C:\downloads\trash\3.txt:5:aaa
    C:\downloads\trash\3.txt:9:aaa

  • #57064

    Participant
    Points: 8
    Rank: Member

    You're doing nothing wrong. That's what Select-String is made for. If you just want the first match:

    Select-String -Path "c:\downloads\trash\*" -Pattern "aaa" | Select-Object -First 1

The topic ‘select-string NOT working’ is closed to new replies.