Powershell 在哈希表中查找键(ContainsKey)不工作

Powershell 在哈希表中查找键(ContainsKey)不工作,powershell,Powershell,我正在加载一个哈希表,其中包含以下内容: $htA = dir | Where-Object {$_.Name -match "\.output\.[A-Z]-[0-9]\.csv"} | select name,length 当我尝试搜索关键字列时,如: $htA.ContainsKey($h.name) 我得到了这个超级整洁的错误: 方法调用失败,因为[Selected.System.IO.FileInfo]不包含名为“ContainsKey”的方法。 At line:3 char:9

我正在加载一个哈希表,其中包含以下内容:

$htA = dir | Where-Object {$_.Name -match "\.output\.[A-Z]-[0-9]\.csv"} | select name,length
当我尝试搜索关键字列时,如:

$htA.ContainsKey($h.name)
我得到了这个超级整洁的错误:

方法调用失败,因为[Selected.System.IO.FileInfo]不包含名为“ContainsKey”的方法。

At line:3 char:9
 IF ($htA.ContainsKey($h.name) -eq $false) {

     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : InvalidOperation: (ContainsKey:String) [], RuntimeException
+ FullyQualifiedErrorId : MethodNotFound**

那么这不是一个合法的哈希表吗?

$htA被创建为对其强制转换的类型,在本例中是System.IO.FileInfo。试试这个:

$htA = @{} # Initialises as an empty hash table
foreach ($file in (dir | Where-Object {$_.Name -match "\.output\.[A-Z]-[0-9]\.csv"} | select name,length))
{
$htA.Add($file.name, $file.length) # Populate hash table
}