Get-ADComputer weird output

Welcome Forums General PowerShell Q&A Get-ADComputer weird output

This topic contains 7 replies, has 4 voices, and was last updated by

 
Participant
4 years ago.

  • Author
    Posts
  • #19458

    Participant
    Points: 0
    Rank: Member

    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 πŸ™‚ Is this "fixed" in some version of PowerShell? I'm testing this in 4.0.

    Thank you,

  • #19461

    Participant
    Points: 0
    Rank: Member

    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.GetType()
    
    $Computer = Get-ADComputer -SearchBase "DC=domain,DC=local" -Fileter "*"
    $Computer[1].GetType()
    

    Return the same results.

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

  • #19462

    Participant
    Points: 0
    Rank: Member

    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 πŸ™‚

    • #19464

      Participant
      Points: 0
      Rank: Member

      Thank you πŸ™‚

  • #19463

    Participant
    Points: 0
    Rank: Member

    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.

  • #19471

    Participant
    Points: 0
    Rank: Member

    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

    • #19481

      Participant
      Points: 0
      Rank: Member

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

  • #19496

    Participant
    Points: 0
    Rank: Member

    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?

The topic ‘Get-ADComputer weird output’ is closed to new replies.