我以为我得到了所有的容器
$containers = Get-ChildItem -path $Path -recurse | ? {$_.psIscontainer -eq $true}
,
但它似乎只返回我的 $Path
的子目录。我真的希望 $containers
包含 $Path
及其子目录。我试过这个:
$containers = Get-Item -path $Path | ? {$_.psIscontainer -eq $true}
$containers += Get-ChildItem -path $Path -recurse | ? {$_.psIscontainer -eq $true}
但它不允许我这样做。我使用 Get-ChildItem
是不是错了,或者我如何通过将 Get-Item 和 Get-ChildItem 与 -递归?
最佳答案
在您第一次调用 get-item 时,您没有将结果存储在数组中(因为它只有一项)。这意味着您不能在 get-childitem
行中将数组附加到它。通过将结果包装在 @()
中,只需强制您的容器变量成为一个数组,如下所示:
$containers = @(Get-Item -path $Path | ? {$_.psIscontainer})
$containers += Get-ChildItem -path $Path -recurse | ? {$_.psIscontainer}
关于powershell - 将 Get-Item 与 Get-ChildItem 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11941316/