user1576013
user1576013

Reputation: 13

How to list subfolders matching PATTERN in PowerShell?

My script is used to find all subfolders with criteria like name matching under the root folder:

$Allsubfolders= gci c:\Root -recurse | Group-Object directoryname | select name
    foreach ($folder in $Allsubfolders) {
    if (($folder.name) -like "*NY*"){
        $list = @(  
            @{source = "$($folder.name)"; dest=c:\dest}     
        )
}

The problem I have with my script is that I cannot grep all folders matching NY, I can only grep one of them. It seems like it stop on the first occurrence. How can I resolve this?

Upvotes: 1

Views: 1036

Answers (1)

tommymaynard
tommymaynard

Reputation: 2152

Can you do something list this?

$Allsubfolders = Get-ChildItem -Recurse | Select-String -Pattern 'NY'

Upvotes: 1

Related Questions