0

Here is the code which displays information about the monitor connected to my computer which works fine.

Get-WmiObject -Namespace root\wmi -Class WmiMonitorBasicDisplayParams |
select @{ N="Computer"; E={$_.__SERVER} }, InstanceName,
@{ N="Horizonal"; E={[System.Math]::Round(($_.MaxHorizontalImageSize/2.54), 2)} },
@{ N="Vertical"; E={[System.Math]::Round(($_.MaxVerticalImageSize/2.54), 2)} },
@{N="Size"; E={[System.Math]::Round(([System.Math]::Sqrt([System.Math]::Pow($_.MaxHorizontalImageSize, 2) + [System.Math]::Pow($_.MaxVerticalImageSize, 2))/2.54),2)} },
@{N="Ratio";E={[System.Math]::Round(($_.MaxHorizontalImageSize)/($_.MaxVerticalImageSize),2)} }

What I need is to separate the output as Monitor 1, Monitor 2 and Monitor 3 based on the number of monitors connected. The current code will output the following:

Computer     : HOME-HP
InstanceName : DISPLAY\HWP3183\4&1badc1af&0&UID200195_0
Horizonal    : 18.9
Vertical     : 10.63
Size         : 21.68
Ratio        : 1.78

Computer     : HOME-HP
InstanceName : DISPLAY\HPN3394\4&1badc1af&0&UID224795_0
Horizonal    : 20.87
Vertical     : 11.81
Size         : 23.98
Ratio        : 1.77

But I want is below

Computer     : HOME-HP
Monitor 1 InstanceName : DISPLAY\HWP3183\4&1badc1af&0&UID200195_0
Monitor 1 Horizonal    : 18.9
Monitor 1 Vertical     : 10.63
Monitor 1 Size         : 21.68
Monitor 1 Ratio        : 1.78

Computer     : HOME-HP
Monitor 2 InstanceName : DISPLAY\HPN3394\4&1badc1af&0&UID224795_0
Monitor 2 Horizonal    : 20.87
Monitor 2 Vertical     : 11.81
Monitor 2 Size         : 23.98
Monitor 2 Ratio        : 1.77
AdminOfThings
  • 23,946
  • 4
  • 17
  • 27
Anit
  • 3
  • 3

1 Answers1

0

The simple solution is to iterate through the array and format the output:

$Monitors = Get-WmiObject -Namespace root\wmi -Class WmiMonitorBasicDisplayParams

$Count = 0
ForEach($Monitor in $Monitors){
    $Count += 1
    $Monitor | select @{ N="Computer"; E={$_.__SERVER} }, `
    @{N="Monitor $Count InstanceName"; E={$_.InstanceName}},`
    @{N="Monitor $Count Horizonal"; E={[System.Math]::Round(($_.MaxHorizontalImageSize/2.54), 2)} }, `
    @{N="Monitor $Count Vertical"; E={[System.Math]::Round(($_.MaxVerticalImageSize/2.54), 2)} }, 
    @{N="Monitor $Count Size"; E={[System.Math]::Round(([System.Math]::Sqrt([System.Math]::Pow($_.MaxHorizontalImageSize, 2) + [System.Math]::Pow($_.MaxVerticalImageSize, 2))/2.54),2)} },`
    @{N="Monitor $Count Ratio";E={[System.Math]::Round(($_.MaxHorizontalImageSize)/($_.MaxVerticalImageSize),2)} }

}

Edit:

To add the Manufacture Week and Serial Number, you have to join the information inside the for loop:

$Monitors = Get-WmiObject -Namespace root\wmi -Class WmiMonitorBasicDisplayParams

$MonitorIDs = Get-WmiObject -Namespace root\wmi -Class WmiMonitorID

$Count = 0
ForEach($Monitor in $Monitors){
    $Count += 1

    $MonitorId = ($MonitorIDs | ?{$_.InstanceName -eq $Monitor.InstanceName})

    $Monitor | select @{ N="Computer"; E={$_.__SERVER} }, `
    @{N="Monitor $Count InstanceName"; E={$_.InstanceName}},`
    @{N="Monitor $Count Horizonal"; E={[System.Math]::Round(($_.MaxHorizontalImageSize/2.54), 2)} }, `
    @{N="Monitor $Count Vertical"; E={[System.Math]::Round(($_.MaxVerticalImageSize/2.54), 2)} }, 
    @{N="Monitor $Count Size"; E={[System.Math]::Round(([System.Math]::Sqrt([System.Math]::Pow($_.MaxHorizontalImageSize, 2) + [System.Math]::Pow($_.MaxVerticalImageSize, 2))/2.54),2)} },`
    @{N="Monitor $Count Ratio";E={[System.Math]::Round(($_.MaxHorizontalImageSize)/($_.MaxVerticalImageSize),2)} },`
    @{N="Monitor $Count WeekOfManufacture"; E={$MonitorId.WeekOfManufacture}},`
    @{N="Monitor $Count SerialNumberID"; E={$MonitorId.SerialNumberID}},`
    @{N="Monitor $Count SerialNumber"; E={[System.Text.Encoding]::ASCII.GetString($MonitorId.SerialNumberID)}}

}
HAL9256
  • 12,384
  • 1
  • 34
  • 46
  • Thanks. That worked exactly how I want. I have one more question. I would also like to add manufacture week and serial number to the output which uses a different class. https://learn.microsoft.com/en-us/windows/desktop/wmicoreprov/wmimonitorid – Anit Apr 10 '19 at 15:11
  • I have edited the code to add the Manufacture week and serial number. – HAL9256 Apr 10 '19 at 15:42
  • Thank you! Appreciate your help. 5 Star. – Anit Apr 11 '19 at 16:05