# If a user has no disabled PCs in AD, I get {} as result not 0. Why?

**URL:** https://forums.powershell.org/t/if-a-user-has-no-disabled-pcs-in-ad-i-get-as-result-not-0-why/7551
**Category:** PowerShell Help
**Created:** [November 18, 2016, 4:05am UTC](https://forums.powershell.org/t/if-a-user-has-no-disabled-pcs-in-ad-i-get-as-result-not-0-why/7551 "2016-11-18T04:05:46Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![ps-fan](https://avatars.discourse-cdn.com/v4/letter/p/e8c25b/32.png) [@ps-fan](https://forums.powershell.org/u/ps-fan)
#### Post date: [November 18, 2016, 4:05am UTC](https://forums.powershell.org/t/if-a-user-has-no-disabled-pcs-in-ad-i-get-as-result-not-0-why/7551/1 "2016-11-18T04:05:46Z")

</div>

```
#This script counts pcs per user. Enabled and disabled.
#If a user has no disabled PCs in AD, I get {} as result not 0. Why?

$samaccountnames = "abc","def","ghi"
foreach ($samaccountname in $samaccountnames)
{
    $comps = Get-ADComputer -Filter * | Where-Object { $_.distinguishedname -like "*$samaccountname*"}
    $count = $comps.count
    $count_enabled = ($comps | Where-Object {$_.enabled -eq $true }).count
    $count_disabled = ($comps | Where-Object {$_.enabled -eq $false}).count
    
    $properties = [ordered]@{'user'=$samaccountname;
                    'PCs'=$count;
                    'PCs_enabled'=$count_enabled;
                    'PCs_disabled'=$count_disabled;
                    }
    $obj = New-Object -TypeName PSObject -Property $properties
    Write-Output $obj
}
```

Thanks.

---

<div class="post-metadata">

### Author: ![juancrl](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@juancrl](https://forums.powershell.org/u/juancrl)
#### Post date: [November 18, 2016, 4:40am UTC](https://forums.powershell.org/t/if-a-user-has-no-disabled-pcs-in-ad-i-get-as-result-not-0-why/7551/2 "2016-11-18T04:40:36Z")

</div>

Because the .Count property only exists on an ARRAY and the result of $Comps | Where-Object  
can sometimes be an array (when 2 or more objects), $null (empty collection), or the computer object itself (which fortunately has not a .Count property).

Suggestion : Use ALWAYS Set-StrictMode -version Latest at the beginning of all your scripts, you’ll see the error.

One Possible Solution : Use a CAST to an array (the @ operator) as in

```
$count_enabled = @($comps | Where-Object {$_.enabled -eq $true }).count
```

Others : Use Measure-Object instead of .Count, or predefine the type of the variable as a System.Object.

Note : PS3 does this cast automatically, but only for some object types, and only for the .Count property.  
Risky… and showing you don’t really understand PowerShell pipeline model.

---

<div class="post-metadata">

### Author: ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)
#### Post date: [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/if-a-user-has-no-disabled-pcs-in-ad-i-get-as-result-not-0-why/7551/3 "2024-05-16T20:39:59Z")

</div>


