How to test for $null array in PowerShell

前端 未结 6 980
感情败类
感情败类 2020-12-08 00:24

I\'m using an array variable in PowerShell 2.0. If it does not have a value, it will be $null, which I can test for successfully:

PS C:\\> [array]$foo =          


        
6条回答
  •  一生所求
    2020-12-08 01:00

    If your solution requires returning 0 instead of true/false, I've found this to be useful:

    PS C:\> [array]$foo = $null
    PS C:\> ($foo | Measure-Object).Count
    0
    

    This operation is different from the count property of the array, because Measure-Object is counting objects. Since there are none, it will return 0.

提交回复
热议问题