Beniamino_Baggins Beniamino_Baggins - 3 months ago 9
PowerShell Question

Locate file in folder further down the hierarchy

I need to locate a .exe file and run it, only knowing the directory which is a few levels higher than the .exe file

EG If I have this folder structure:

test\bin\Debug\netcoreapp1.0\win7-x64\program.exe

and I only know the test folder, I need to get the path to program.exe, without knowing the name of the .exe file either (but there is only one .exe file).

So something like:

gcm *.exe test


Where test is the source folder to look in.

How do I do this?

Answer

This sounds like what you're looking for:

$path = 'test'
$program = Get-ChildItem -Path $path -Filter *.exe -Recurse
& $program.FullName

The & operator there is the call operator; it can run an executable given the full path to it.