Reputation: 527
It's quite weird that Write-Host
doesn't seem to parse variable's property.
PS C:\Users\x> $v=@{item1="kkk";item2="aaa"}
PS C:\Users\x> $v.Keys|%{$v.Item($_)}
kkk
aaa
PS C:\Users\x> Write-Host "$v.Count elements"
System.Collections.Hashtable.Count elements
PS C:\Users\x> $v.Count
2
PS C:\Users\x> $v
Name Value
---- -----
item1 kkk
item2 aaa
You could see, $v
is a hashtable and
$v.Count
prints 2. But why does
Write-Host "$v.Count"
print out System.Collections.Hashtable.Count
? This is not what I expected.
Upvotes: 3
Views: 2838
Reputation: 22122
Text representation of $v
is System.Collections.Hashtable
([string]$v
or "$v"
). And "$v.Count elements"
means {text representation of $v}.Count elements
not {text representation of $v.Count} elements
, so for me it is expected that you get System.Collections.Hashtable.Count elements
as result.
Write-Host
is not responsible for expanding double quoted strings. It done by PowerShell before it invoke Write-Host
.
"$v.Count"
prints 2
For me it prints System.Collections.Hashtable.Count
. 2
printed by $v.Count
not by "$v.Count"
.
You should use "$($v.Count) elements"
to get expected result.
Upvotes: 7