PowerShell: How to Find All Folders Matching a Pattern


Often you may want to use PowerShell to find all folders and subfolders in a directory that match a particular pattern.

You can use the following syntax to do so:

$my_path= "c:\users\bobbi\data1"

(Dir -Recurse -Directory -Filter "*team*" -Path $my_path).FullName

This particular example returns the full path to each folder and subfolder located in the directory specified by the $my_path variable that contains the pattern “team” anywhere in the folder name.

The following example shows how to use this syntax in practice.

Example: How to Find All Folders Matching a Pattern in PowerShell

Suppose that we have a directory located at the following path:

  • c:\users\bobbi\data1

Suppose that we would like to find all folders in this directory that contain the pattern “team” anywhere in the folder name.

Before we do so, we can use the tree command to first view the folder structure of this directory:

Just from looking at this folder structure we can see that there are two folders that contain “team” in the folder name.

We can use the following syntax to retrieve the path to each of these folders:

$my_path= "c:\users\bobbi\data1"

(Dir -Recurse -Directory -Filter "*team*" -Path $my_path).FullName

The following screenshot shows how to use this syntax in practice:

PowerShell find all folders matching pattern

Notice that this returns the following folder paths:

  • c:\users\bobbi\data1\managers\team_admin
  • c:\users\bobbi\data1\managers\team_roster

Notice that both of these folders contain the name “team” in the folder name.

Feel free to replace “team” with any pattern that you’d like.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Copy and Rename a File
PowerShell: How to Use Copy-Item and Create New Directory
PowerShell: How to Use Copy-Item and Exclude Folders

Leave a Reply