0

I am trying to recursively search a directory for a specified file that contains an .exe and then save that directory path to then use in a variable later.

I figured that using:

ls -recurse -filter "PsExec.exe" | sort name | ft directory,name

will display the directory that contains the .exe. Is there a way that I can save off the directory so that I can later use it to reference the .exe in another part of my script? The .exe will be saved in a folder dynamically named so that's why I can't hardcode it's path.

4
  • 1
    Is that file guaranteed to be unique? Or will it return a set of files?
    – SirPentor
    Feb 26, 2013 at 20:24
  • Can you pass the result in the array $items =ls -recurse -filter "PsExec.exe" | sort name | ft directory,name
    – Hiten004
    Feb 26, 2013 at 20:27
  • You don't want to pass the results of format-table into $items, but the rest is a good idea.
    – SirPentor
    Feb 26, 2013 at 20:29
  • yes, the file is guaranteed to be unique Feb 26, 2013 at 20:43

1 Answer 1

3

If you just want to save the full path to a single instance of the file, you can use

$path = ls -recurse -filter "PsExec.exe" | select -first 1 | select -expand FullName

Or you can save multiple properties if you want

$file = ls -recurse -filter "PsExec.exe" | select -first 1 | select Name,FullName,Directory

$file.Name
$file.FullName
$file.Directory
2
  • Is there a way for it to search hidden folders too? Feb 26, 2013 at 21:15
  • Add -Force flag to the ls call
    – latkin
    Feb 26, 2013 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.