0

here is the script I've created. I have downloaded the 'PSSearch' package and when I goto commands 'Search-Index' is one of the available commands

 $computers = @([some computer])
 $destination = "[some path]"

foreach ($computer in $computers) {
        $Path = Set-Location [path on computer]
        $keywords= @('"word 1"','word2','word3','word4')
        $dirlist = Get-ChildItem -Recurse -Force $Path -ErrorAction Continue

    foreach($word in $keywords) {
         $SearchResults = Search-Index $word
         $dirlist | Where-Object {$_.Name -match $SearchResults}  | Select-Object Name,FullName | format-Table * -AutoSize |
         Export-Csv $destination\FoundFiles.csv -nti -Append

         $cui = ($dirlist | Where-Object {$_.Name -match $SearchResults})
         Copy-Item $cui -Destination $destination - Append
    
}

}

What is happening is I'm getting all files and folders from the location (not just the ones I'm searching for)

The problem could be that I don't know how this line should be scripted

$cui = ($dirlist | Where-Object {$_.Name -match $SearchResults})
Talthing
  • 25
  • 1
  • 5
  • 1
    As an aside: `Format-*` cmdlets emit output objects whose sole purpose is to provide _formatting instructions_ to PowerShell's for-display output-formatting system. In short: only ever use `Format-*` cmdlets to format data _for display_, never for subsequent _programmatic processing_ - see [this answer](https://stackoverflow.com/a/55174715/45375) for more information. – mklement0 Jun 26 '22 at 21:59
  • Why use such a function at all? If all you're after is to find files with a certain keyword in their name, why not use the `-Include` parameter of Get-ChildItem (and/or refine afterwards with a Where-Object clause) – Theo Jun 27 '22 at 09:26
  • @Theo - I'm trying to find the keywords inside the files – Talthing Jul 01 '22 at 02:45

0 Answers0