Need to make search in PowerShell script faster

Who can help to make this search faster? With this code the search takes a few days.

Search_Names.csv (about 10k names)

Need_This_Long_Strings.csv (about 180k strings and it's 50MB)

$TimeStamp = Get-Date -Format {yyyy.MM.dd_hh.mm.ss}
$SearchNames = gc D:\Search_Names.csv
$WhereSearch = gc D:\Need_This_Long_Strings.csv
$Val = 0
 
foreach ($SearchName in $SearchNames)
{
       $WhereSearch | Where{$_ | Select-String -Pattern "$SearchName.*"} | Out-File D:\Find_in_Search_File_$TimeStamp.log -Append
       $Val = $Val + 1
}
"Count of matches - $Val" |Out-File D:\Find_in_Search_File_$TimeStamp.log -Append

Upvotes: 0

Views: 135

Answers (1)

I found the solution myself. Just made the data (Need_This_Long_Strings.csv) into an array. Now this code search takes a about 20 min.

$TimeStamp = Get-Date -Format {yyyy.MM.dd_hh.mm.ss}
$SearchNames = Get-Content D:\Search_Names.csv
$WhereSearch = Import-Csv D:\Need_This_Long_Strings.csv -Delimiter ";"
$SearchArray = New-Object System.Collections.ArrayList($null)
$SearchArray.AddRange($WhereSearch)
$Val = 0

foreach ($_ in $SearchArray)
{
       if ($SearchNames -contains $_.FullName)
       {
             $_ | Export-Csv D:\Find_in_Search_File_$TimeStamp.csv -Delimiter ";" -Append
             $Val = $Val + 1
       }
}
"Count of matches - $Val" |Out-File D:\Find_in_Search_File_$TimeStamp.log -Append

Upvotes: 0

Related Questions