Question

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.

Was it helpful?

Solution

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top