# Return computer names from a "foreach" statement

**URL:** <https://forums.powershell.org/t/return-computer-names-from-a-foreach-statement/4922>\
**Category:** PowerShell Help\
**Created:** [September 25, 2015, 3:19am UTC](https://forums.powershell.org/t/return-computer-names-from-a-foreach-statement/4922 "2015-09-25T03:19:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rocky-cabral1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rocky-cabral1/32/543_2.png) [@rocky-cabral1](https://forums.powershell.org/u/rocky-cabral1)\
**Post date:** [September 25, 2015, 3:19am UTC](https://forums.powershell.org/t/return-computer-names-from-a-foreach-statement/4922/1 "2015-09-25T03:19:03Z")

</div>

I’m trying to retrieve all of our Dell monitor serial numbers, I found a script that accomplishes that for a single computer name but needed to tweak it to accept a list of computers. I got the output to display but doesn’t show the computers that are associated with their attached monitors.  
I’m sure this is a simple tweak of the script to provide the computer name and the monitors attached to that computer. But, just can’t get it.

Here is the script:

$PCs = gc d:\test.txt

$PCs | foreach {

```
    $ActiveMonitors = Get-WmiObject -Namespace "ROOT\WMI" -Query "SELECT * FROM WmiMonitorID WHERE Active='True'"
    $monitorInfo = @()

    foreach ($monitor in $ActiveMonitors) {
        $mon = @{}
        $manufacturer = $null
        $product = $null
        $serial = $null
        $name = $null
        $week = $null
        $year = $null

        # These are returned as ascii values, '00' padded.
        # We don't want the padding because we can't .Trim() it.
        foreach($ch in $monitor.ManufacturerName) {
            if($ch -ne '00') {
                $manufacturer += [char]$ch
            }
        }
         foreach($ch in $monitor.ProductCodeID) {
            if($ch -ne '00') {
                $product += [char]$ch
            }
        }
         foreach($ch in $monitor.SerialNumberID) {
            if($ch -ne '00') {
                $serial += [char]$ch
            }
        }
         foreach($ch in $monitor.UserFriendlyName) {
            if($ch -ne '00') {
                $name += [char]$ch
            }
        }

        $mon.Add("Manufacturer",$manufacturer)
        $mon.Add("ProductCode",$product)
        $mon.Add("SerialNumber",$serial)

        # These few are normal strings
        #Adding "PCs" variable wasn't the right answer but thought I try.
        $mon.Add("Name",$PCs) 
        $mon.Add("Week",$monitor.WeekOfManufacture)
        $mon.Add("Year",$monitor.YearOfManufacture)

        $monitorInfo += $mon
    }
    $monitorInfo

```

}

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [September 25, 2015, 3:37am UTC](https://forums.powershell.org/t/return-computer-names-from-a-foreach-statement/4922/2 "2015-09-25T03:37:11Z")

</div>

Try below updated script:

```
$PCs = gc d:\test.txt

$PCs | foreach {

    $ComputerName = $_
    $ActiveMonitors = Get-WmiObject -Namespace "ROOT\WMI" -Query "SELECT * FROM WmiMonitorID WHERE Active='True'" -ComputerName $ComputerName
    
    $monitorInfo = @{
        ComputerName = $ComputerName
        Monitors = @()
    }

    foreach ($monitor in $ActiveMonitors) {
        $mon = @{ }
        $manufacturer = $null
        $product = $null
        $serial = $null
        $name = $null
        $week = $null
        $year = $null

        # These are returned as ascii values, '00' padded.
        # We don't want the padding because we can't .Trim() it.
        foreach($ch in $monitor.ManufacturerName) {
            if($ch -ne '00') {
            $manufacturer += [char]$ch
            }
        }
        foreach($ch in $monitor.ProductCodeID) {
            if($ch -ne '00') {
            $product += [char]$ch
            }
        }
        foreach($ch in $monitor.SerialNumberID) {
            if($ch -ne '00') {
            $serial += [char]$ch
            }
        }
        foreach($ch in $monitor.UserFriendlyName) {
            if($ch -ne '00') {
            $name += [char]$ch
            }
        }

        $mon.Add("Manufacturer",$manufacturer)
        $mon.Add("ProductCode",$product)
        $mon.Add("SerialNumber",$serial)

        # These few are normal strings
        #Adding "PCs" variable wasn't the right answer but thought I try.
        $mon.Add("Name",$PCs)
        $mon.Add("Week",$monitor.WeekOfManufacture)
        $mon.Add("Year",$monitor.YearOfManufacture)

        $monitorInfo.Monitors += $mon
    }
    $monitorInfo
}
```

---

<div class="post-metadata">

**Author:** ![rocky-cabral1](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rocky-cabral1/32/543_2.png) [@rocky-cabral1](https://forums.powershell.org/u/rocky-cabral1)\
**Post date:** [September 25, 2015, 3:47am UTC](https://forums.powershell.org/t/return-computer-names-from-a-foreach-statement/4922/3 "2015-09-25T03:47:39Z")

</div>

Thanks Daniel…that worked!

I did make another change on this line:  
$mon.Add(“Name”,$PCs)  
to this…  
$mon.Add(“Name”,$\_)

---

<div class="post-metadata">

**Author:** ![daniel-krebs](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/daniel-krebs/32/484_2.png) [@daniel-krebs](https://forums.powershell.org/u/daniel-krebs)\
**Post date:** [September 25, 2015, 5:21pm UTC](https://forums.powershell.org/t/return-computer-names-from-a-foreach-statement/4922/4 "2015-09-25T17:21:12Z")

</div>

Cool. Sorry I missed that line. You probably don’t need it because the monitorInfo hash table already returns the computer name.

---

<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:44pm UTC](https://forums.powershell.org/t/return-computer-names-from-a-foreach-statement/4922/5 "2024-05-16T20:44:45Z")

</div>


