Link to home
Start Free TrialLog in
Avatar of credog
credog

asked on

Powershell Find Folders

I need to find folders on Windows 7 systems that have a certain string in their name. The following works well:
Get-ChildItem c:\Folder -recurse | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match 'foo' -or $_.Name -match 'bar'} 

Open in new window

But what I would like to do is search from the C:\ without specifying a sub-directory.  When I do this:
Get-ChildItem c:\ -recurse | Where-Object {$_.PSIsContainer -eq $true -and $_.Name -match 'foo' -or $_.Name -match 'bar'} 

Open in new window

The command seems to prematurely end.  It doesn't take that long to run.  I have tried using -ErrorAction SilentlyContinue, but it doesn't seem to make difference other than suppressing the folder access denied issues.   My guess is that something is causing it to quit, but I'm having trouble discovering why.  
I need to use a Get-* type of powershell commands since it will be used within a third party app that only allows Get-* powershell commands.
ASKER CERTIFIED SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of credog
credog

ASKER

Dan: Thanks for catching the logic error, unfortunately it didn't solve the original issue, but at least the command is correct.  

oBdA: Thanks for the tips, but the majority of systems that this needs to run on is using 2.0.  I'll experiment with the filter option to see how that speeds things up.

Still need to figure out why I can't search from the C:\ and I need to use something like C:\users instead.
Your code (copy pasted) works both on Windows 7 and Windows 8 machines.

My guess is that you're running it under a user that has no rights to the root folder.
These machines should be updated. Seriously. PS 2.0 is from 2009, and PS 5.0 is available even for Windows 7; I'd consider 3.0 the bare minimum.
That said, collecting each and every file/folder and then filtering is the most expensive part of that script.
Using the filter might help, but not as much as -Directory would.
That said, I'd recommend a different, but simple approach and use cmd.exe from PS to get the folders you're interested in.
& cmd.exe /c "cd /d C:\&dir /s /b *foo* *bar*"

Open in new window

Avatar of credog

ASKER

Running as a Domain user, but it still errors.   It ended up being a directory under C:\windows\ that was causing the issue.  On the powershell console it just provides an access denied and keeps going.  When using the same command in wrapper for auditing a system the process quits.  This is a problem with the auditing software it seems.  Thanks
Avatar of credog

ASKER

None of these solutions fixed the issue I was having since the problem was related to a wrapper process. But, the solutions provided valuable feedback that will be used for future projects.