Link to home
Start Free TrialLog in
Avatar of sirichaiphumirat
sirichaiphumiratFlag for United States of America

asked on

finding file in powershell

I'm pretty new to powershell,

How would i use powershell to find a file and get the path. And also is it possible to use the path in a variable?

for example. I want to search for a file on my computer called  mytestfile.exe
and get the path of its location. And put that location into a variable.

I was trying to use get-childitme -recurse -include,  but i'm not sure if i am using it correctly

I seem to be able to get the  directory but how would i put the path into a variable

thanks
ASKER CERTIFIED SOLUTION
Avatar of BT15
BT15
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sirichaiphumirat

ASKER

Great thanks that worked, how would you do it differently if it was more then one file.
that depends on what you intend to do with your results:
This will deplay the results on the screen:


$files = get-childitem c:\ -recurse | ? {$_.name -eq "mytestfile.exe"}

write-host there are $files.count matching that name

foreach ($file in $files){
$directory = $file.directory
$fulldirectorypath = $file.directory.fullname
$fullfilepath = $file.fullname
write-host $file.name
write-host the directory path is $fulldirectorypath
}
Thank you, you been very help full.
you are welcome