Issue

An issue while fetching more records with search query using PowerShell.

Error

Method invocation failed because [System.Management.Automation.PSObject] doesn't contain a method named `op_Addition'. 

Code Used

001

002

003

004

005

006

$Search = Get-Content C:\DraftSearch.txt

foreach($string in $search)

{

$file += Get-ChildItem C:\Draft_CVS -Recurse | Select-String -Pattern $string | Select Path , Pattern

$file | Export-Csv C:\Draft_CVS.csv

}

 

Cause:

PowerShell was trying to collect collection of objects / custom objects and breaks.

 

Solution

$File = @()

This needs to be added before the for each loop.

Download

Refer to the script in the Gallery