Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get powershell to display all paths where a certain file can be found on a drive

Tags:

powershell

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
like image 417
KdgDev Avatar asked Apr 18 '09 03:04

KdgDev


People also ask

How do I get the file path in PowerShell?

To get the full path of the file in PowerShell, use the Get-ChildItem to get files in the directory and pass the output to foreach-object to iterate over the file and get the full name of the file.

How do I find a specific file in PowerShell?

Get-ChildItem cmdlet in PowerShell is used to get items in one or more specified locations. Using Get-ChildItem, you can find files. You can easily find files by name, and location, search file for string, or find file locations using a match pattern.

How do I get a list of files in a directory and subfolders using PowerShell?

PowerShell utilizes the “Get-ChildItem” command for listing files of a directory. The “dir” in the Windows command prompt and “Get-ChildItem” in PowerShell perform the same function.

How do I get a list of files in a folder PowerShell?

Like the Windows command line, Windows PowerShell can use the dir command to list files in the current directory. PowerShell can also use the ls and gci commands to list files in a different format.


1 Answers

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-ChildItem -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-ChildItem:

Get-ChildItem | Get-Member
like image 173
Arnold Spence Avatar answered Sep 28 '22 07:09

Arnold Spence