Return computer names from a "foreach" statement

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!

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
}

Thanks Daniel…that worked!

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

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