# Search Folders in all directories

**URL:** <https://forums.powershell.org/t/search-folders-in-all-directories/19762>\
**Category:** PowerShell Help\
**Created:** [June 30, 2022, 1:23pm UTC](https://forums.powershell.org/t/search-folders-in-all-directories/19762 "2022-06-30T13:23:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![tami](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tami/32/2473_2.png) [@tami](https://forums.powershell.org/u/tami)\
**Post date:** [June 30, 2022, 1:23pm UTC](https://forums.powershell.org/t/search-folders-in-all-directories/19762/1 "2022-06-30T13:23:45Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 30, 2022, 1:33pm UTC](https://forums.powershell.org/t/search-folders-in-all-directories/19762/2 "2022-06-30T13:33:34Z")

</div>

> [@tami](#):
>
> This code searches for a specific folder or file only on the C directory.

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

> [@tami](#):
>
> from the D:\ as well as H:, how can I produce a result which searches from all directories

When you carefully (re-)read the help for

> **[Get-ChildItem (Microsoft.PowerShell.Management) - PowerShell](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.2)**
>
> The Get-ChildItem cmdlet gets the items in one or more specified locations. If the item is a container, it gets the items inside the container, known as child items. You can use the Recurse parameter to get items in all child containers and use the...

you will read that

> -Path
> 
> 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. 😉

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 PowerShell.org](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/2X/b/bee50c628238f2c076c0504efb6b15f2fb11f002.gif) \<---- Click 👆🏽 😉

---

<div class="post-metadata">

**Author:** ![tami](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tami/32/2473_2.png) [@tami](https://forums.powershell.org/u/tami)\
**Post date:** [June 30, 2022, 2:03pm UTC](https://forums.powershell.org/t/search-folders-in-all-directories/19762/3 "2022-06-30T14:03:53Z")

</div>

Sorry,I’m still confused

```auto
$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

```auto
C:\Users\HP\hello
C:\Users\HP\new\hello

```

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 30, 2022, 2:34pm UTC](https://forums.powershell.org/t/search-folders-in-all-directories/19762/4 "2022-06-30T14:34:05Z")

</div>

You’re constantly mixing files and folder?! 🫤 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:

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

```

> [@tami](#):
>
> How can I specify that I want the first one’s path.

You use `Select-Object -First 1`

---

<div class="post-metadata">

**Author:** ![tami](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tami/32/2473_2.png) [@tami](https://forums.powershell.org/u/tami)\
**Post date:** [June 30, 2022, 2:59pm UTC](https://forums.powershell.org/t/search-folders-in-all-directories/19762/5 "2022-06-30T14:59:06Z")

</div>

> 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?

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 30, 2022, 3:16pm UTC](https://forums.powershell.org/t/search-folders-in-all-directories/19762/6 "2022-06-30T15:16:17Z")

</div>

> [@tami](#):
>
> 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 .

What’s the criteria? 🤔

> [@tami](#):
>
> that PC may not contain the H: directory will it cause an error?

Of course.

---

<div class="post-metadata">

**Author:** ![tami](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tami/32/2473_2.png) [@tami](https://forums.powershell.org/u/tami)\
**Post date:** [June 30, 2022, 3:30pm UTC](https://forums.powershell.org/t/search-folders-in-all-directories/19762/7 "2022-06-30T15:30:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 30, 2022, 3:33pm UTC](https://forums.powershell.org/t/search-folders-in-all-directories/19762/8 "2022-06-30T15:33:00Z")

</div>

In your example from above both paths match this criteria. 😉

---

<div class="post-metadata">

**Author:** ![tami](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tami/32/2473_2.png) [@tami](https://forums.powershell.org/u/tami)\
**Post date:** [June 30, 2022, 3:51pm UTC](https://forums.powershell.org/t/search-folders-in-all-directories/19762/9 "2022-06-30T15:51:50Z")

</div>

Then,the path which contains new

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [June 30, 2022, 4:01pm UTC](https://forums.powershell.org/t/search-folders-in-all-directories/19762/10 "2022-06-30T16:01:15Z")

</div>

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

> **[Where-Object (Microsoft.PowerShell.Core) - PowerShell](https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/where-object?view=powershell-7.2)**
>
> The Where-Object cmdlet selects objects that have particular property values from the collection of objects that are passed to it. For example, you can use the Where-Object cmdlet to select files that were created after a certain date, events with a...

and filter for whatever criteria you need.

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

---

<div class="post-metadata">

**Author:** ![tami](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tami/32/2473_2.png) [@tami](https://forums.powershell.org/u/tami)\
**Post date:** [June 30, 2022, 4:16pm UTC](https://forums.powershell.org/t/search-folders-in-all-directories/19762/11 "2022-06-30T16:16:00Z")

</div>

Thanks a lot,will try and let you know😇

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:17pm UTC](https://forums.powershell.org/t/search-folders-in-all-directories/19762/12 "2024-05-16T20:17:44Z")

</div>

Closed on May 16, 2024, 8:17pm UTC


