tags:

views:

588

answers:

2

I'm trying to build a function that will show me all path's where a certain filename is located. The function would take one parameter, that being the file name. The result would be either a list of all paths, or a message saying there's no such file on the system.

I'm new to Powershell, and I'm not getting the syntax just yet. I've tried this:

Get-ChildItem -Path -Include notepad.exe

But that threw an error message. I'm currently trying:

$i="notepad.exe"

Foreach ($i in Get-ChildItem c:\ -Recurse){echo -Path}

Started that now, it's still running, don't know what'll happen, really.

EDIT: echo'd an enormous amount of lines that just say "-Path"...

Can anybody help with this problem? I'm running Powershell 1.0 by the way.

So, to explain what I wish to see when executing this command, here is an example of what I expect after looking for *.txt:

C:/foo.txt
C:/A/foobar.txt
C:/A1/foo.txt

And so on, listing the path to all .txt files on my harddrive. Only the paths, one per line, no extra info needed.

EDIT2:

I've done it. I'm gonna leave this question up for those who make look for this in the future.

The function I used was this(this specific example will hand you a list of all .zip files on your harddrive, edit where needed):

Get-ChildItem -Path c:\ -Include "*.zip" -Recurse -Force -Name > c:\listOfPaths.txt

This created a file called listOfPaths.txt on my C:\ folder and this contained a list of all occurences of any file ending with .zip in all subfolders of my harddrive.

The "c:\" bit isn't mentioned, but I don't mind.

EDIT3:

thanks capar for a more complete version.

Here is capar's code(or how I got it to work, since Get-Children doesn't work in 1.0)

Get-ChildItem -Path c:\ -Recurse *.txt | Select-Object -Property FullName
+1  A: 

ls c:\ -r | ? {$_.name -eq "notepad.exe"}

zvolkov
solved, check the answer if you like.
WebDevHobo
+2  A: 

Since it's Friday night, I decided to play with Power Shell to see if I can help :) This comes pretty close to what you are asking for I think:

Get-Children -Path c:\ -Recurse *.txt | Select-Object -Property FullName

If it helps, this command will list the properties of any object that will be returned by Get-Children:

Get-Children | Get-Member
Arnold Spence
Powershell error: Get-Children is not a recognised cmdlet, function...Are you running Powershell 2.0 by any chance?
WebDevHobo
Used Get-ChildItem and it worked like a charm. Awsome, thanks for the help
WebDevHobo
Welcome. Glad it worked out.
Arnold Spence