title: Objects with no '.Count' Property - use of @() (array subexpression operator) vs. [Array] cast
body: <p>I am trying to perform some simple if statements, but all of the newer cmdlets that are based upon [Microsoft.Management.Infrastructure.CimInstance] don't seem to expose a .count method?</p>
  
  <pre><code>$Disks = Get-Disk
  $Disks.Count
  </code></pre>
  
  <p>Doesn't return anything. I found that I can cast this as an [array], which makes it returns a .NET .count method as expected.</p>
  
  <pre><code>[Array]$Disks = Get-Disk
  $Disks.Count
  </code></pre>
  
  <p>This works without directly casting it as an array for previous cmdlets:</p>
  
  <pre><code>(Get-Services).Count
  </code></pre>
  
  <p>What is the recommended way to get around this?</p>
  
  <p>An example that doesn't work:</p>
  
  <pre><code>$PageDisk = Get-Disk | Where {($_.IsBoot -eq $False) -and ($_.IsSystem -eq $False)}
    If ($PageDisk.Count -lt 1) {Write-Host "No suitable drives."; Continue}
     Else If ($PageDisk.Count -gt 1) {Write-Host "Too many drives found, manually select it."}
     Else If ($PageDisk.Count -eq 1) { Do X }
  </code></pre>
  
  <p>Option A (Cast as Array):</p>
  
  <pre><code>[Array]$PageDisk = Get-Disk | Where {($_.IsBoot -eq $False) -and ($_.IsSystem -eq $False)}
    If ($PageDisk.Count -lt 1) {Write-Host "No suitable drives."; Continue}
     Else If ($PageDisk.Count -gt 1) {Write-Host "Too many drives found, manually select it."}
     Else If ($PageDisk.Count -eq 1) { Do X }
  </code></pre>
  
  <p>Option B (Use Array Indexes):</p>
  
  <pre><code> $PageDisk = Get-Disk | Where {($_.IsBoot -eq $False) -and ($_.IsSystem -eq $False)}
    If ($PageDisk[0] -eq $Null) {Write-Host "No suitable drives."; Continue}
     Else If ($PageDisk[1] -ne $Null) {Write-Host "Too many drives found, manually select it."}
     Else If (($PageDisk[0] -ne $Null) -and (PageDisk[1] -eq $Null)) { Do X }
  </code></pre>
  
  <p>Option C (Array) -Thanks to @PetSerAl :</p>
  
  <pre><code>$PageDisk = @(Get-Disk | Where {($_.IsBoot -eq $False) -and ($_.IsSystem -eq $False)})
    If ($PageDisk.Count -lt 1) {Write-Host "No suitable drives."; Continue}
     Else If ($PageDisk.Count -gt 1) {Write-Host "Too many drives found, manually select it."}
     Else If ($PageDisk.Count -eq 1) { Do X }
  </code></pre>
  
  <p>What is the reason for CIM based cmdlets not exposing the .Count method? What is the recommended way to handle this? Option B seems convoluted to me, and hard to read. Option A works, but shouldn't powershell cast this as an array for me? Am I going about this in entirely the wrong way?</p>