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]
The following script will solve the problem i had. Thanks for carlpett’s useful inputs