finding file in powershell

Posted on 2013-05-21
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
  • 3
  • 2

Accepted Solution

BT15 earned 500 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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help Decipher Powershell Script 5 27
Import ProxyAddress and other fields 13 48
powershell add-content , set-content 2 20
Office 365 Script for reporting 4 18
Utilizing an array to gracefully append to a list of EmailAddresses
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

816 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now