Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8485051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:32:16+00:00 2026-06-10T20:32:16+00:00

I am trying to search strings in files and display the file name which

  • 0

I am trying to search strings in files and display the file name which contains the strings

I have written a script like below.

Function GetFileContainsData ([string] $Folderpath,[string] $filename, [string] $Data) { 

    $Files=Get-ChildItem $Folderpath -include $filename -recurse | Select-String -pattern $Data | group path | select name
    return,$Files
}

$configFiles= GetFileContainsData "C:\MyApplication" "web.config" "localhost"
Foreach ($file in $configFiles) { Add-Content -Path "Filelist.txt" -Value $file.Name}

This script writes all the filename which contains the string “localhost” into Filelist.txt.

I would like to find more than one string. If i pass an array

$stringstofind=("localhost","$env:ComputerName")
Foreach ($strings in $stringsToFind) {
    $configFiles= GetFileContainsData $Installpath "web.config" $strings
    Foreach ($file in $configFiles) { Add-Content -Path "Filelist.txt" -Value $file.Name}
}

It will look for each string in array with the list of files and update it. If the same file has both the strings , it will have 2 entries of that file in Filelist.txt.

Is it possible to find more than one string in a file and list out the name of the file? [so that redundant entry of file name may eliminated]

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T20:32:18+00:00Added an answer on June 10, 2026 at 8:32 pm

    The following script will solve the problem i had. Thanks for carlpett’s useful inputs

    <#
        .NAME
            Get-FilenameHasString
        .SYNOPSIS
            To search strings in file and return the filenames.
        .DESCRIPTION
            This script will be useful to find strings in list of files.
            It will eliminate the duplicate filenames
        .PARAMETER Filenames
            This parameter will be having filenames where the string needs to be searched.
        .PARAMETER FolderPath
            This parameter will be having folder where it will look for the filenames
        .PARAMETER StringsToFind
            This parameter will be having the strings to be searched
        .EXAMPLE
           .\Get-FilenameHasString -Filenames "Web.config" -FolderPath "C:\Program Files (x86)\MyProj" -$StringsToFind ("$env:ComputerName","localhost")
           To get the  list of web.config files which is there in Folder path having StringsTofind. The result will be saved in Filelist.log by default.
        #>
    
    Param ( 
        $Filenames="web.config",
        $FolderPath="C:\Program Files (x86)\MyProj",
        $StringsToFind=("$env:ComputerName","localhost"),
        $Logfile="FileList.log"
        )
    
    $configFiles= Get-ChildItem $FolderPath -Include $Filenames -Recurse | Select-String -Pattern $StringsToFind | Group path | select name | Get-Unique -AsString
    Foreach ($file in $configFiles) { Add-Content -Path $Logfile -Value $file.Name}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to search for files which matches following expression with sed [a-zA-Z0-9]{1,10}\s{1,5}\
I have a textbox and a search button i trying to search file names
I'm trying to write a small Python script to parse the .strings file in
I am trying to search for multiple strings within a larger string and if
Im trying to search two of the fields in my table. (Database table contains
I'm trying to search rather big files for a certain string and return its
I am trying to search the C:\ drive for all files with a certain
I am trying to search a list of files and only perform work on
string[] files = Directory.GetFiles(tb_dir.Text, tb_filter.Text, SearchOption.AllDirectories); I'm trying to search through a directory and
i am trying search data in xml file. if found, it will popup MessageBox

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.