powershellpowershell-4.0

List files recursively within directories that start with a specified letter


I'm trying to get all *.html files within certain directories via PowerShell. Folder structure looks like this:

folder
  subfolder1
  subfolder2
  subfolder3
  subfolder4
  ...

Subfolders are in alphabetical order. There exist folders from a to z. I now want to get all .html files within the folders that start with the letter b.

I tried

Get-ChildItem -Path *.html -Recurse -Force

this gives me all html files from all folders. But how can I restrict the path, so that it only checks subdirectories that start with b?


Solution

  • I'm not sure if this is the most efficient, but you could do something like this:

    Get-ChildItem -Directory b* | Select -ExpandProperty FullName | % {Get-ChildItem $_\*.html}
    

    Or depending on what you want, you could do Get-ChildItem -Directory b* -Recurse as well.

    You might need to format Get-ChildItem $_ depending on what you want output to look like, since it writes the directory by default.