Search Folders in all directories

Get-Childitem –Path C:\ -Recurse –force -ErrorAction SilentlyContinue

This code searches for a specific folder or file only on the C directory.
Suppose I need all files named, for example ‘new.txt’ from the D:\ as well as H:, how can I produce a result which searches from all directories

Actually not. This code lists ALL files and folders on drive c: - not a specific one. :wink:

When you carefully (re-)read the help for

you will read that


Specifies a path to one or more locations. Wildcards are accepted. The default location is the current directory (.).

So you simply list all drives you want to search. :wink:

BTW: When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.

Thanks in advance

How to format code in <---- Click :point_up_2:t4: :wink:

Sorry,I’m still confused

$filename = 'hello'
if($filename  -ceq 'hello'){
    $filename = 'hello'
else{ (Write-Output "File Not Found")}
$searchinfolder= 'C:'
$root=Get-ChildItem -Path $searchinfolder -Filter $filename -Recurse | %{$_.FullName}

Write-Output $root

This is the code I used to find the folder named 'hello".
How can I add other directories like ‘D,H’ in the $searchfolder variable.
In addition to that, If there are more than one file with the same name in a directory.Lets say


How can I specify that I want the first one’s path.
Thanks in advance.

You’re constantly mixing files and folder?! :face_with_diagonal_mouth: What are you looking for?

Get-ChildItem has two switch parameters where you can specify to look only for files or folders.

Assumed you’re looking for the folder with the name “hello” on drives C:, D: and H: you can use something like this:

Get-ChildItem -Path C:,D:,H: -Filter 'hello' -Recurse -Directory |
    Select-Object -Property FullName

You use Select-Object -First 1

You use Select-Object -First 1
But If I run the same program in another computer, my required path may come in 3rd or 4th depending on that PC’s configuration. In that case, how can I filter out the required path . Sorry If somethings may confuse you,I am still not good with terms.
And other thing, if I use the code in other computer and that PC may not contain the H: directory will it cause an error?

What’s the criteria? :thinking:

Of course.

Let’s say,i want the path which contains a folder 'users' in it.

In your example from above both paths match this criteria. :wink:

Then,the path which contains new

OK, Actually it would be the best option to provide a search path as close to the searched folder as possible. So when you search a profile folder you should start the search in C:\Users and not in C:. That would even speed up you script.

If you want to filter your results for particular criteria later on you can use

and filter for whatever criteria you need.

Please read the help completely including the examples to learn hot to use it.

Thanks a lot,will try and let you know😇