Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


finding file in powershell

Posted on 2013-05-21
Medium Priority
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

Question by:sirichaiphumirat
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2

Accepted Solution

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

for example:

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

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

Hope that helps

Author Comment

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

Expert Comment

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 | ? {$ -eq "mytestfile.exe"}

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

foreach ($file in $files){
$directory = $
$fulldirectorypath = $
$fullfilepath = $file.fullname
write-host $
write-host the directory path is $fulldirectorypath

Author Comment

ID: 39185943
Thank you, you been very help full.

Expert Comment

ID: 39185952
you are welcome

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

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.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

722 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