1

I'm having this code:

    $Paths = 'C:\' , 'P:\' , "\\fril01\ufr$\$env:username"

    $torEXE = Get-childitem -path $paths -recurse -Exclude $ExcludePaths -erroraction 'silentlycontinue'  | where-object {$_.name -eq "Tor.exe"}

    if ($torEXE.Exists) {$answer = 1}

To check for file tor.exe, but as you can see this check could take some time. the could be a chance the check will find tor.exe on the first few seconds but will continue checkink all the paths. i want it to halt immidietly after it found tor.exe and not continue searching for it. how can it be done?

Shahar Weiss
  • 141
  • 1
  • 2
  • 11

1 Answers1

5

Stick |Select-Object -First $N at the end of your pipeline to make it stop executing after the first $N objects reaches Select-Object:

$torEXE = Get-ChildItem -Path $paths -Recurse -Exclude $ExcludePaths -ErrorAction 'silentlycontinue'  | Where-Object {$_.Name -eq "Tor.exe"} |Select -First 1
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • it will still go throu allllll the folders (even after it finds the file) and in the end it will pipe only the first finding – Shahar Weiss Jun 09 '20 at 16:55
  • 3
    It will not, `Select-Object -First 1` will [cause the pipeline to stop](https://stackoverflow.com/questions/60790273/select-object-first-affects-prior-cmdlet-in-the-pipeline) abruptly after the first `tor.exe` is found, thanks to [the nature of pipeline command processing](https://stackoverflow.com/questions/48522415/are-the-cmdlets-in-a-pipeline-executing-in-parallel) in PowerShell – Mathias R. Jessen Jun 09 '20 at 17:04
  • Thank you so much :) – Shahar Weiss Jun 09 '20 at 17:18