I've made a most unfortunate typo costing me quite some precious time:
$errors.Count
This returns "0
", even if there are errors, because the variable name should be singular. This does work:
$error.clear() # To ensure a correct repro
Copy-Item asdf fdsa # Will show an error
$error.Count # Will output "1"
However, I now want to know why $errors.Count
gave me anything at all, and why it gave me "0
". So I went on to do some testing, and got the following results:
$asdf.Count # Will output "0"
$nrOfEinsteinsInSpace.Count # Will output "0"
$a = 0; $a.Count; # Will output "1"
$b = 2; $a.Count; # Will output "1"
$x = 1,2,3; $x.Count; # Will output "3"
And gathering even more data to be able to ask a sensible question here I did:
$true.Count # Will output "1"
$false.Count # Will output "1"
So we have the following different cases:
- Array(like) variables, where
.Count
will output the number of items. - Non-existent variables, where
.Count
will output "0". - Declared variables, where
.Count
will output "1". - Built-in variables, where
.Count
will output "1".
Case 2, 3, and 4 don't make any sense to me (yet). What is going on here? Where is this documented? How does the .Count
property work?