Reputation: 12433
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?
Upvotes: 0
Views: 51
Reputation: 3390
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.
Upvotes: 1
Reputation: 200293
gcm
is an alias for Get-Command
. You're looking for gci
(Get-ChildItem
). Use the parameter -Recurse
to recurse into subfolders and -Filter
to filter for files with a particular extension:
Get-ChildItem test -Filter *.exe -Recurse
Note that, as @Guvante pointed out, this might return more than one executable (or even none at all), so you need to decide how you want to handle all these cases (run all executables, the first, the last, none, fail with an error, fail silently, ...).
Once you have a matching file you can run it for instance via the call operator:
$exe = Get-ChildItem ... | Select-Object -First 1 -Expand FullName
& $exe
Upvotes: 1