Get-ADComputer weird output

I just played with cmd-let Get-ADComputer. I found out that if I run:

$Computer = Get-ADComputer -SearchBase "DC=domain,DC=local" -Fileter "*"

$Computer is System.Array. It contains 29 records.

But if I define filter to filter out only one object:

$Computer = Get-ADComputer -SearchBase "DC=domain,DC=local" -Fileter 'Name -like "*OneComputerName*"'

$Computer is Microsoft.ActiveDirectory.Management.ADAccount.

This is really confusing. I know how to fix it, just define type of the object:

[array]$Computer = Get-ADComputer -SearchBase "DC=domain,DC=local" -Fileter 'Name -like "*OneComputerName*"'

But this not behaviour I would expect :slight_smile: Is this “fixed” in some version of PowerShell? I’m testing this in 4.0.

Thank you,

This is default behaviour.

The array is just a container for items of the Microsoft.ActiveDirectory.Management.ADAccount type

$Computer = Get-ADComputer -SearchBase "DC=domain,DC=local" -Fileter 'Name -like "*OneComputerName*"'
$Computer = Get-ADComputer -SearchBase "DC=domain,DC=local" -Fileter "*"

Return the same results.

You can still use the result in operations such as ForEach, so you shouldnt need to adjust your code.

As Tim points out, this is not a bug, but the expected behaviour when working with a loosely typed language. It will “guess” the type based on the data it get assigned. You can always strongly type your variables, like you have discovered yourself :slight_smile:

Tim Pringle wrote:
can still use the result in

That’s corret, but when you want to list total items and you want to use .Count property of array it doesn’t work on other than array object. I think I will stay with defining type of variables.

Thank you :slight_smile:

Don’t forget you can use Measure-Object to count objects in the pipeline as well. This way it doesn’t matter if it is an array or not, you’ll always get the right count:

Get-ADComputer -SearchBase “DC=domain,DC=local” -Fileter ‘Name -like “OneComputerName”’ | Measure-Object

Maybe this is not a case, but isn’t Measure-Object more CPU intensive than variable definition?

It might be but whether that matters really depends on the scenario. Is this something that will be done many times in a script, or just once?