Shahar Weiss
Shahar Weiss

Reputation: 149

make get-Childitem stop if a file is found

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?

Upvotes: 1

Views: 1415

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174505

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

Upvotes: 6

Related Questions