# Get-Adcomputer output

**URL:** <https://forums.powershell.org/t/get-adcomputer-output/8517>\
**Category:** PowerShell Help\
**Created:** [April 26, 2017, 2:08pm UTC](https://forums.powershell.org/t/get-adcomputer-output/8517 "2017-04-26T14:08:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tuxnician](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tuxnician/32/494_2.png) [@tuxnician](https://forums.powershell.org/u/tuxnician)\
**Post date:** [April 26, 2017, 2:08pm UTC](https://forums.powershell.org/t/get-adcomputer-output/8517/1 "2017-04-26T14:08:22Z")

</div>

I’m using Get-Adcomputer to pull computers from AD that have 133 in the name. The command works but I need to put each computer’s name into a variable so that I can use it to get information from each computer.

```
$computername =get-adcomputer -filter {Name -like "sd133*"} | select -expandproperty name 
$bios = gwmi -class win32_bios | select -expand smbiosbiosversion
$model = gwmi -class win32_computersystem | select -expand model

foreach ($Name in $ComputerName) {

$objAverage = New-Object System.Object
$objAverage | Add-Member -type NoteProperty -name Name -value $computername
$objAverage | Add-Member -type NoteProperty -name Bios -value $bios
$objAverage | Add-Member -type NoteProperty -Name Model -Value $model

}
```

Output:

Name Bios Model

* * *

{SD133-01, SD133-02, SD133-03, SD133-04…} A16 OptiPlex 760

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [April 26, 2017, 2:22pm UTC](https://forums.powershell.org/t/get-adcomputer-output/8517/2 "2017-04-26T14:22:04Z")

</div>

I suspect you’re looking for something like this:

```
$computers = get-adcomputer -filter {Name -like “sd133*”} | Select-Object -ExpandProperty name
$ResultObject = New-Object System.Collections.Generic.List[object]

foreach ($ComputerName in $computers){  
$bios = Get-WmiObject -Class win32\_bios -ComputerName $ComputerName | Select-Object -ExpandProperty smbiosbiosversion  
$model = Get-WmiObject -Class win32\_computersystem -ComputerName $ComputerName | Select-Object -ExpandProperty model  
$newobj = [PSCustomObject]@{  
ComputerName = $ComputerName  
BIOS = $bios  
Model = $model  
}  
$ResultObject.Add($newobj)  
}

$ResultObject
```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [April 26, 2017, 2:32pm UTC](https://forums.powershell.org/t/get-adcomputer-output/8517/3 "2017-04-26T14:32:42Z")

</div>

You’ve got several things wrong. Your WMI calls are getting the local computer information, not the remote computer, so they have to be inside your loop. Next you are referencing $ComputerName, which is an array of objects versus $Name being the current object in your loop. Start with this basic example:

```
$computername =get-adcomputer -filter {Name -like "sd133*"} | select -expandproperty name 

$results = foreach ($Name in $ComputerName) {

    $bios = gwmi -class win32_bios -ComputerName $Name| select -expandproperty smbiosbiosversion
    $model = gwmi -class win32_computersystem -ComputerName $Name| select -expandproperty model

    New-Object -TypeName PSObject -Property @{
        ComputerName = $Name
        Bios = $bios
        Model = $model
    }

}

$results
```

What you are attempting to do is a very common Powershell task. I would recommend getting a book on Powershell or minimally looking at other examples. The example above does not have any error handling if a computer is offline, but as I was saying this a very common task so examples should be easy to find.

---

<div class="post-metadata">

**Author:** ![tuxnician](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tuxnician/32/494_2.png) [@tuxnician](https://forums.powershell.org/u/tuxnician)\
**Post date:** [May 2, 2017, 8:11am UTC](https://forums.powershell.org/t/get-adcomputer-output/8517/4 "2017-05-02T08:11:44Z")

</div>

The script works and I’ve added error handling but the output is in the wrong order.

I get Bios Name Model where I’d like to have the Name first. I’m not sure how to change that?

---

<div class="post-metadata">

**Author:** ![tuxnician](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/tuxnician/32/494_2.png) [@tuxnician](https://forums.powershell.org/u/tuxnician)\
**Post date:** [May 2, 2017, 8:14am UTC](https://forums.powershell.org/t/get-adcomputer-output/8517/5 "2017-05-02T08:14:11Z")

</div>

Thanks! This will get what I need and I do see my mistakes.

---

<div class="post-metadata">

**Author:** ![Olaf](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/olaf/32/992_2.png) [@Olaf](https://forums.powershell.org/u/Olaf)\
**Post date:** [May 2, 2017, 8:55am UTC](https://forums.powershell.org/t/get-adcomputer-output/8517/6 "2017-05-02T08:55:29Z")

</div>

Objects are by default “unsorted” and “unordered”. If you like to maintain a special order you will have to sort it when you output it or you can create it already ordered:

```
New-Object -TypeName PSObject -Property ([ORDERED]@{
ComputerName = $Name
Bios = $bios
Model = $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:38pm UTC](https://forums.powershell.org/t/get-adcomputer-output/8517/7 "2024-05-16T20:38:50Z")

</div>

Closed on May 16, 2024, 8:38pm UTC


