How does the Count property work in Powershell?

后端 未结 3 2050
南方客
南方客 2020-12-09 19:10

I\'ve made a most unfortunate typo costing me quite some precious time:

$errors.Count

This returns \"0\", even if there are er

3条回答
  •  死守一世寂寞
    2020-12-09 19:19

    Here is how i think it works:

    Case 1: In Arrays the .Count Property actually links to the .Length property which shows the number of Items in the Array

    Case 2: Non-exitent variables get automatically created by powershell and initialized with value $null

    Case 3 / 4: On this one i am not exactly sure why it happens but since neither String nor Int or boolean Objects have a .Count property i could imagine that the Property is inherited by a parent-object.

    The behaviour suggests that the variable is treated as array so with 1 Value assigned the output will be 1, without a value the result will be 0.

    Edit: For the sake of completeness here is the Link to the Documentation: Technet, thanks @David Brabant

提交回复
热议问题