PowerShell live documentation

PSCustomObject

Count property

When not running in strict mode, most objects in PowerShell have a Count property:

  1. $array = @(1, 2, 3)
  2. Write-Output "Array count: $($array.Count)"
  3. $dictionary = @{A=1; B=2; C=3; D=4}
  4. Write-Output "Dictionary count: $($dictionary.Count)"
  5. $number = 5
  6. Write-Output "Number count: $($number.Count)"
  7. $string = 'hello there'
  8. Write-Output "String count: $($string.Count)"
  9. $process = (Get-Process)[0]
  10. Write-Output "Process object count: $($process.Count)"
Stdout
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.

  1. $object = [PSCustomObject]@{A=1; B=2; C=3; D=4; E=5}
  2. Write-Output "PSCustomObject count: $($object.Count)"
Stdout
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.

  1. Set-StrictMode -Version Latest
  2. try {
  3.     $array = @(1, 2, 3)
  4.     Write-Output "Array count: $($array.Count)"
  5. } catch {
  6.     Write-Output "Array count threw: $_"
  7. }
  8. try {
  9.     $dictionary = @{A=1; B=2; C=3; D=4}
  10.     Write-Output "Dictionary count: $($dictionary.Count)"
  11. } catch {
  12.     Write-Output "Dictionary count threw: $_"
  13. }
  14. try {
  15.     $number = 5
  16.     Write-Output "Number count: $($number.Count)"
  17. } catch {
  18.     Write-Output "Number count threw: $_"
  19. }
  20. try {
  21.     $string = 'hello there'
  22.     Write-Output "String count: $($string.Count)"
  23. } catch {
  24.     Write-Output "String count threw: $_"
  25. }
  26. try {
  27.     $process = (Get-Process)[0]
  28.     Write-Output "Process object count: $($process.Count)"
  29. } catch {
  30.     Write-Output "Process object count threw: $_"
  31. }
  32. try {
  33.     $object = [PSCustomObject]@{A=1; B=2; C=3; D=4; E=5}
  34.     Write-Output "PSCustomObject count: $($object.Count)"
  35. } catch {
  36.     Write-Output "PSCustomObject count threw: $_"
  37. }
Stdout
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