Home > Software engineering >  Searching for only folders that contain a certain folder using powershell
Searching for only folders that contain a certain folder using powershell

Time:06-23

I am trying to use powershell to update some programs for my company. I am writing a script to do so (as instructed). When I install the new version of the program on the machines, it also requires me to 'upgrade' existing folders to match the new version of the software. I need to find all of the folders that contain a certain hidden folder(let the name of said folder be .blah). I am trying to use the get-childitem command, with -path [drives to check] -Recurse -Directory -Force -EA SilentlyContinue. However, I am not sure how to filter correctly to only find folders that contain the .blah folder inside of it. Help would be very much appreciated.

CodePudding user response:

You need to nest Get-ChildItem calls:

# Note: "." refers to the *current* directory
#       Adjust as needed.
Get-ChildItem -LiteralPath . -Recurse -Directory -Force -ErrorAction Ignore |
  Where-Object {
    $_ | Get-ChildItem -LiteralPath .blah -Directory -Force -ErrorAction Ignore
  }
  • The first Get-ChildItem call outputs all directories (-Directory) in the entire directory subtree (-Recurse), including hidden ones (-Force), ignoring any errors (such as from lack of permissions, -ErrorAction Ignore).

  • The second call, which is used as a filter via the Where-Object cmdlet, checks each input directory for containing a (potentially hidden) child directory named .blah.

CodePudding user response:

You can use GCI with -directory parameter so you are only getting dir's, pipe to where-object to check that the fullName property contains the desired folder name

get-childitem -path "D:\" -Recurse -Directory -Force -EA SilentlyContinue | ? {$_.FullName -like "*.blah*"}
  • Related