Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to search for a folder with PowerShell

Tags:

powershell

I would like to search for a folder in a specific directory and subdirectorys.

I tried googling it, but didn't really find any usefull examples.

like image 316
mads Avatar asked Sep 30 '13 10:09

mads


People also ask

How do I find a folder in PowerShell?

On a Windows computer from PowerShell or cmd.exe, you can display a graphical view of a directory structure with the tree.com command. To get a list of directories, use the Directory parameter or the Attributes parameter with the Directory property. You can use the Recurse parameter with Directory.

How do I search for a file in a directory in PowerShell?

To search the content in the file in PowerShell, you need to first get the content from the file using Get-Content command and then you need to add Select-String pipeline command. In the below example, we need to search the lines which contain the Get word. You can use multiple patterns to search for the result.

What is the PowerShell command for dir?

The dir cmdlet in Windows PowerShell generates a list of File and Folder objects, which PowerShell formats into a text listing after the fact.


2 Answers

Get-ChildItem C:\test -recurse | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match "keyword"} 

I believe there's no dedicated cmdlet for searching files.

Edit in response to @Notorious comment: Since Powershell 3.0 this is much easier, since switches -Directory and -File were added to Get-ChildItem. So if you want it short you've got:

ls c:\test *key* -Recurse -Directory 

With command alias and tab-completion for switches it's a snap. I just missed that the first time.

like image 194
AdamL Avatar answered Sep 22 '22 12:09

AdamL


Here is my version, which is only slightly different:

gci -Recurse -Filter "your_folder_name" -Directory -ErrorAction SilentlyContinue -Path "C:\" 

some more info:

-Filter "your_folder_name" 

From documentation: Filters are more efficient than other parameters. The provider applies filter when the cmdlet gets the objects rather than having PowerShell filter the objects after they're retrieved. The filter string is passed to the .NET API to enumerate files. The API only supports * and ? wildcards.

-Directory  

Only examine directories, also could be -File

-ErrorAction SilentlyContinue  

Silences any warnings

-Path "C:\" 

Specifies a path to start searching from

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7

like image 20
aturc Avatar answered Sep 25 '22 12:09

aturc