Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

finding file in powershell

Posted on 2013-05-21
5
Medium Priority
?
297 Views
Last Modified: 2013-05-21
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
0
Comment
Question by:sirichaiphumirat
  • 3
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
BT15 earned 2000 total points
ID: 39185633
do you want the full filepath?

for example:

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

$directory = $file.directory
$fulldirectorypath = $file.directory.fullname
$fullfilepath = $file.fullname
obviously, if you have more than one file returned, we would have to change this a bit.

Hope that helps
0
 

Author Comment

by:sirichaiphumirat
ID: 39185898
Great thanks that worked, how would you do it differently if it was more then one file.
0
 
LVL 7

Expert Comment

by:BT15
ID: 39185924
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
}
0
 

Author Comment

by:sirichaiphumirat
ID: 39185943
Thank you, you been very help full.
0
 
LVL 7

Expert Comment

by:BT15
ID: 39185952
you are welcome
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Loops Section Overview
Screencast - Getting to Know the Pipeline

783 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question