String count with Powershell

Gaurav Kumar 21 Reputation points
2021-01-19T14:18:10.577+00:00

I would like to write a script to count the occurrences of each string from File A, in bunch of files from remote servers. I am new to Powershell and don't have a clue how to achieve this, appreciate any help on this.

For example-

FileA.txt:
Orange
Mango
Banana

FileB.txt:
\computer-1\1.txt
\computer-2\2.txt
\computer-3\3.txt

Expecting output:
Orange 3
Mango 5
Banana 7

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,901 Reputation points
    2021-01-19T15:44:06.19+00:00

    Is this a homework project??? I hope not. You won't learn much this way.

    $n = get-content C:\junk\FileA.txt
    $h = @{}
    "1.txt","2.txt","3.txt" |
        ForEach-Object{
            Get-Content $_ |
                ForEach-Object{
                    if ($n -contains $_){
                        $h.$_++
                    }
                }
        }
    $h
    
    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.