Collecting Multiple Monitor Information

H Kaur 41 Reputation points
2021-11-23T02:50:23.01+00:00

Hi,

Asking this question here because my previous one is awaiting moderator review :(

We have users using more than 2 monitors at work and we need to collect the monitor information for them and append to a csv. I have the below script:

$query = "Select * from WmiMonitorID"

if ($objWmi -isnot [system.array]) { $objWmi = @($objWmi) }  

$objWmi = Get-WmiObject -Query $query -namespace root\WMI -ComputerName $env:COMPUTERNAME -ErrorAction SilentlyContinue  

$CSVFile = "C:\Users\user\Desktop\computer1.csv"

Function ConvertTo-Char
(
$Array
)
{
$Output = ""
ForEach($char in $Array)
{ $Output += [char]$char -join ""
}
return $Output
}

$Results = ForEach ($Monitor in $objWmi)
{
New-Object PSObject -Property @{

    UserFriendlyName = ConvertTo-Char($Monitor.userfriendlyname)  

SerialNumber = ConvertTo-Char($Monitor.serialnumberid)

}  

}

$Results | Select-Object UserFriendlyName | Export-Csv -Path $CSVFile -Append -Force

I get the output as below:

151654-image.png

What I want to achieve is an output as below:

151564-image.png

So for each users's $env:COMPUTERNAME, the monitor names (userfriendlyname) will go into 1 column in csv and will need to be separated by commas.

I was trying to achieve this using arrays but haven't been able to work it out.

Can someone please help with this?

Thanks in advance.

P.S. : Sorry for creating a new post for this.

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,627 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 47,596 Reputation points
    2021-11-23T03:35:16.567+00:00

    Something like this might work:

    $UFN = @()
    $objwmi |
        ForEach-Object{
            $UFN += ConvertTo-Char($_.userfriendlyname)
            #SerialNumber = ConvertTo-Char($_.serialnumberid)
        }
        [PSCustomObject]@{
            UserFriendlyName = ($UFN -join ';')
        } | Export-Csv -Path $CSVFile -Append
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.