由于使用 -Recurse 标志时,Get-ChildItem 的 -Exclude 参数不会对子文件夹进行过滤,因此请参阅使用 get-childitem-exclude-parameter-in-powershell 中的其他无法排除目录的内容
但 -Exclude 参数可用于过滤掉根级别的文件夹
我写了自己的递归函数:
function Get-ChildItem-Recurse() {
[cmdletbinding()]
Param(
[parameter(ValueFromPipelineByPropertyName = $true)]
[alias('FullName')]
[string[]] $Path,
[string] $Filter,
[string[]] $Exclude,
[string[]] $Include,
[switch] $Recurse = $true,
[switch] $File = $false
)
Process {
ForEach ( $P in $Path ) {
Get-ChildItem -Path $P -Filter $Filter -Include $Include -Exclude $Exclude | ForEach-Object {
if ( -not ( $File -and $_.PSIsContainer ) ) {
$_
}
if ( $Recurse -and $_.PSIsContainer ) {
$_ …
Run Code Online (Sandbox Code Playgroud)