0

EngConI need help getting info out of the Select-String command. I need to find which items from $array are inside the engcon.pbo file. It would be most useful if the found results were displayed on the console or even better, in a .txt file.

In the full code, there are 297 items in the array (#0 and #295 included).

######
#Mine#
######


$TargetFile   = "C:\PowershellScripts\EngCon.pbo"

$array = @("Comprehension", "Outspeed", "Marsileaceae", "Chalybeate")

$i = 0

while ($i -le 295)
{

$SearchString = $array[$i]

Select-String $TargetFile -pattern $SearchString

$i = $i + 1

}

#######
#Yours#
#######

$array = @($array = @("Comprehension", "Outspeed", "Marsileaceae", "Chalybeate")
$found = @{}
Get-Content "C:\PowershellScripts\EngCon.txt" | % {
  $line = $_
  foreach ($item in $array) {
    if ($line -match $item) { $found[$item] = $true }
  }
}

$found.Keys | Out-File "C:\PowershellScripts\results.txt"

If possible could you also provide some good places to learn PS.

After quick testing with "write-host" the results show something in the foreach ($item in $array) is causing the error(ends script instantly), also the sample file I use is just a tester of some array items and some random words, all separated by spaces. As for the code, all I edited was the set of items in $array

FYI, I cannot reveal most of the array items as they are private

"Comprehension random Outspeed hello yours Uncovenable Marsileaceae extreme Runcation Guggle Tribunitious Chalybeate" Is The Full Tester File For All Versions(EngCon.pbo, EngCon.txt And EngCon)

3
  • Obfuscate the array elements then, but show your actual code. Don't tell what you think you did. Commented Jul 1, 2013 at 9:37
  • Why would you put the array definition inside another array definition? I certainly didn't tell you to do that. Change $array = @($array = @("Comprehension", "Outspeed", "Marsileaceae", "Chalybeate") to $array = @("Comprehension", "Outspeed", "Marsileaceae", "Chalybeate") and try again. Commented Jul 1, 2013 at 10:53
  • Thank You VERY Much, works now and worked its purpose for the first time already Commented Jul 2, 2013 at 7:59

1 Answer 1

2

Your Select-String instruction is repeatedly reading $TargetFile. This will adversely affect performance. Try something like this instead:

$array = @(...)

$found = @{}
Get-Content "C:\PowershellScripts\EngCon.pbo" | % {
  $line = $_
  foreach ($item in $array) {
    if ($line -match $item) { $found[$item] = $true }
  }
}

$found.Keys | Out-File "C:\results.txt"
Sign up to request clarification or add additional context in comments.

4 Comments

this returns an empty file, what editing needs to be done other then the array
Not really. The code works fine for me. Is EngCon.pbo actually a text file?
I tried a normal .txt file and a plain file(no .***) and it still comes as an empty file
Please show your code and sample content of the input file. Don't post it as a comment, update your question with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.