fifamaniac04
fifamaniac04

Reputation: 2383

Powershell save directory path from ls search?

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.

Upvotes: 0

Views: 1309

Answers (1)

latkin
latkin

Reputation: 16812

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

Upvotes: 3

Related Questions