当仅存在 1 个项目时,Get-ChildItem 返回字符串

时间:2021-03-31 15:52:51

标签: powershell

我正在使用 $dirs = Get-ChildItem -Path $dest -Name 获取直接文件夹中的文件夹名称列表。

然而,当只有 1 个文件夹时,它返回一个字符串而不是一个数组。

如何强制它每次都返回一个数组?

1 个答案:

答案 0 :(得分:4)

使用 array subexpression operator - @(...)

$dirs = @(Get-ChildItem -Path $dest -Name)

# This will now always hold true
$dirs -is [array]
相关问题