PSCustomObject
Count property
When not running in strict mode, most objects in PowerShell have a
Count
property:
$array = @(1, 2, 3)
Write-Output "Array count: $($array.Count)"
$dictionary = @{A=1; B=2; C=3; D=4}
Write-Output "Dictionary count: $($dictionary.Count)"
$number = 5
Write-Output "Number count: $($number.Count)"
$string = 'hello there'
Write-Output "String count: $($string.Count)"
$process = (Get-Process)[0]
Write-Output "Process object count: $($process.Count)"
Array count: 3 Dictionary count: 4 Number count: String count: Process object count:
Array count: 3 Dictionary count: 4 Number count: 1 String count: 1 Process object count: 1
Notice that all these objects have a Count
(except for
non-collections in version 2). However, PSCustomObject
doesn't have
an accurate Count
of 1 until version 6.1. In version 2, it has the
same count as the dictionary it was created out of.
$object = [PSCustomObject]@{A=1; B=2; C=3; D=4; E=5}
Write-Output "PSCustomObject count: $($object.Count)"
PSCustomObject count: 5
PSCustomObject count:
PSCustomObject count: 1
If we enable strict mode, we get some more differing behavior between versions.
In version 2, we fail to get the count on the number, string, and
process objects. Everything else is the same. In versions 5 and 6.0, we
throw getting the count on everything but the actual collections.
Versions 6.1 and up interestingly have the same behavior as versions 5
and 6.0, except they actually succeed in getting the PSCustomObject
count.
Set-StrictMode -Version Latest
try {
$array = @(1, 2, 3)
Write-Output "Array count: $($array.Count)"
} catch {
Write-Output "Array count threw: $_"
}
try {
$dictionary = @{A=1; B=2; C=3; D=4}
Write-Output "Dictionary count: $($dictionary.Count)"
} catch {
Write-Output "Dictionary count threw: $_"
}
try {
$number = 5
Write-Output "Number count: $($number.Count)"
} catch {
Write-Output "Number count threw: $_"
}
try {
$string = 'hello there'
Write-Output "String count: $($string.Count)"
} catch {
Write-Output "String count threw: $_"
}
try {
$process = (Get-Process)[0]
Write-Output "Process object count: $($process.Count)"
} catch {
Write-Output "Process object count threw: $_"
}
try {
$object = [PSCustomObject]@{A=1; B=2; C=3; D=4; E=5}
Write-Output "PSCustomObject count: $($object.Count)"
} catch {
Write-Output "PSCustomObject count threw: $_"
}
Array count: 3 Dictionary count: 4 Number count threw: Property 'Count' cannot be found on this object. Make sure that it exists. String count threw: Property 'Count' cannot be found on this object. Make sure that it exists. Process object count threw: Property 'Count' cannot be found on this object. Make sure that it exists. PSCustomObject count: 5
Array count: 3 Dictionary count: 4 Number count threw: The property 'Count' cannot be found on this object. Verify that the property exists. String count threw: The property 'Count' cannot be found on this object. Verify that the property exists. Process object count threw: The property 'Count' cannot be found on this object. Verify that the property exists. PSCustomObject count threw: The property 'Count' cannot be found on this object. Verify that the property exists.
Array count: 3 Dictionary count: 4 Number count threw: The property 'Count' cannot be found on this object. Verify that the property exists. String count threw: The property 'Count' cannot be found on this object. Verify that the property exists. Process object count threw: The property 'Count' cannot be found on this object. Verify that the property exists. PSCustomObject count: 1