从 PowerShell 中目录中的特定文件夹获取文件

Get files from specific folders in a directory in PowerShell

我在一个目录中有 4 个文件夹。所有这些文件夹都包含一些文件。我只想列出两个文件夹中的文件名。

C:\MainFolder\FolderOne\FileOne.txt
C:\MainFolder\FolderTwo\FileTwo.txt
C:\MainFolder\FolderThree\FileThree.txt
C:\MainFolder\FolderFour\FileFour.txt

我只想列出 FolderTwo 和 FolderThree 下的文件。

如果我使用 -Recurse -Include "FolderNames",它只会列出文件夹名称。

get-childitem -recurse 'foldertwo','folderthree'
$source="c:\MainFolder" #location of starting directory
$files=@("*.txt", "*.doc") #if you want to include extensions add -include ($files) to get-ChildItem

Get-ChildItem -recurse ($source) -File | Where-Object {$_.PSParentPath -match "Two|Three"}

是不是这样

$arrPath ='C:\MainFolder\FolderTwo','C:\MainFolder\FolderThree'

get-childitem -recurse $arrPath