powershell - 将 Get-Item 与 Get-ChildItem 结合使用?

标签 powershell

我以为我得到了所有的容器 $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/

相关文章:

新实例中的 PowerShell 启动脚本

azure - New-AzureADApplicationPasswordCredential 引发 Authorization_RequestDenied

powershell - 如何从文件名中删除空格

powershell - PowerShell 脚本中的 bool 参数值

powershell - 为什么上游命令被调用了这么多次?

powershell - 在工作站解锁时执行的语法

xml - Powershell 和 XML : How to count specific elements for each node

powershell - powershell Get-Item如何表示目的地是要创建的文件夹?

powershell - 名称以〜开头的Powershell设置属性sh

powershell - 基本 PowerShell 脚本问题 : "Expressions are only allowed as the first element of a pipeline"