Windows Vista Forums

Searching for content in text files with powershell

  1. #1


    snofire Guest

    Searching for content in text files with powershell

    Hey all,

    I need a powershell script that will search through all of the text files in
    a directory, and copy those files that contain a given term into another
    file. This is the line I am having trouble with:

    Get-Content *.* | Select-String -pattern "$term" -context 20,20
    -caseSensitive | Out-File E:\test.txt

    With this, the script locates the terms all right, but because I had to use
    context 20,20 in order to get the whole file where the term lives, it grabs a
    bunch of useless data as well. Is there any way that I can just paste a file
    where the term occurs?

      My System SpecsSystem Spec

  2. #2


    Shay Levi Guest

    Re: Searching for content in text files with powershell


    Hi snofire, try this:

    Select-String -pattern "$term" -context 20,20 -caseSensitive -Path <path>\*.*
    -list | foreach { cat $_.path | Add-Content -Path c:\matches.txt -force}


    Since you copy each matched file full content to another file, I don't think
    the -context 20,20 is necessary.
    If you need to perform recursive search, use:

    get-childitem <path> -filter <wildcard> -recurse | Select-String -pattern
    "$term" ... | foreach { cat $_.path | Add-Content -Path c:\matches.txt -force}

    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com
    Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic



    > Hey all,
    >
    > I need a powershell script that will search through all of the text
    > files in a directory, and copy those files that contain a given term
    > into another file. This is the line I am having trouble with:
    >
    > Get-Content *.* | Select-String -pattern "$term" -context 20,20
    > -caseSensitive | Out-File E:\test.txt
    >
    > With this, the script locates the terms all right, but because I had
    > to use context 20,20 in order to get the whole file where the term
    > lives, it grabs a bunch of useless data as well. Is there any way that
    > I can just paste a file where the term occurs?
    >


      My System SpecsSystem Spec

  3. #3


    Shay Levi Guest

    Re: Searching for content in text files with powershell


    The second command should look like this: (without ... )

    get-childitem <path> -filter <wildcard> -recurse | Select-String -pattern
    "$term" | foreach { cat $_.path | Add-Content -Path c:\matches.txt -force}


    -----
    Shay Levi
    $cript Fanatic
    http://scriptolog.blogspot.com
    Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic



    > Hi snofire, try this:
    >
    > Select-String -pattern "$term" -context 20,20 -caseSensitive -Path
    > <path>\*.* -list | foreach { cat $_.path | Add-Content -Path
    > c:\matches.txt -force}
    >
    > Since you copy each matched file full content to another file, I don't
    > think
    > the -context 20,20 is necessary.
    > If you need to perform recursive search, use:
    > get-childitem <path> -filter <wildcard> -recurse | Select-String
    > -pattern "$term" ... | foreach { cat $_.path | Add-Content -Path
    > c:\matches.txt -force}
    >
    > -----
    > Shay Levi
    > $cript Fanatic
    > http://scriptolog.blogspot.com
    > Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic

    >> Hey all,
    >>
    >> I need a powershell script that will search through all of the text
    >> files in a directory, and copy those files that contain a given term
    >> into another file. This is the line I am having trouble with:
    >>
    >> Get-Content *.* | Select-String -pattern "$term" -context 20,20
    >> -caseSensitive | Out-File E:\test.txt
    >>
    >> With this, the script locates the terms all right, but because I had
    >> to use context 20,20 in order to get the whole file where the term
    >> lives, it grabs a bunch of useless data as well. Is there any way
    >> that I can just paste a file where the term occurs?
    >>


      My System SpecsSystem Spec

  4. #4


    Kiron Guest

    Re: Searching for content in text files with powershell

    Use Select-String's -Quiet switch --it returns a boolean value-- and filter the files that match with Where-Object to set their content to the new file:

    set-content E:\test.txt (
    get-childItem *.txt |
    where {select-string $term $_ -quiet} |
    get-content
    )

    --
    Kiron

      My System SpecsSystem Spec

  5. #5


    Kiron Guest

    Re: Searching for content in text files with powershell

    If $term is present more than once there'll be repeated content, to avoid this use Select-String's -List switch.

    --
    Kiron

      My System SpecsSystem Spec

  6. #6


    snofire Guest

    Re: Searching for content in text files with powershell

    Thank you all for your advice. I'll give it a whirl.

      My System SpecsSystem Spec

Searching for content in text files with powershell

Similar Threads
Thread Forum
Reading text files using powershell
Hi all. I have a particular problem. I have some log files that I want to search. I would like to use powershell to read the files and find a...
PowerShell
searching content within files
I just got vista and it isn't searching content withing files with extension xlr. I have xlr indexed and it still isnt working. Any suggestions?
Vista General
Help searching text within XLS files
I'm at wits' end... in XP you could just go to advanced search and search an entire document for a word or phrase within a specific area (by...
Vista file management
Searching files bases on content.
Hi all, Am trying to do a search for a file based on the content within a file, how ever vista does not appear to return the any files. the...
Vista file management
Searching for files, content on replaceable media (CD)
I cannot figure out how to search for files and content of replaceable media. Example I want to find "tabasco" (file content) on a recipe CD. Search...
Vista General
Searching large text files
I have very large text files (100MB-500MB+) that I need to process in order to extract useful pieces of information. Unfortunately, I can't find an...
PowerShell
The content type text/html of the response message does not match the content type of the binding (text/xml; charset=utf-8)."
System.ServiceModel.ProtocolException was unhandled Is this apparent what this error message would be caused by? This is from the getting started...
Indigo