Needed to create this for a colleague. Get the names of attached monitors via WMI/ PowerShell.
1 |
$((Get-WmiObject WmiMonitorID -Namespace root\wmi) | %{ $Name = $($_.UserFriendlyName -notmatch 0 | ForEach{[char]$_}) -join ""; Write-Output $Name }) -join ";" |
The Get-WmiObject WmiMonitorID -Namespace root\wmi
cmdlet returns output like this for each attached monitor:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
PS C:\WINDOWS\system32> (Get-WmiObject WmiMonitorID -Namespace root\wmi) __GENUS : 2 __CLASS : WmiMonitorID __SUPERCLASS : MSMonitorClass __DYNASTY : MSMonitorClass __RELPATH : WmiMonitorID.InstanceName="DISPLAY\\HWP3028\\4&23ad1ee&0&UID57547520_0" __PROPERTY_COUNT : 9 __DERIVATION : {MSMonitorClass} __SERVER : MYMACHINE __NAMESPACE : root\wmi __PATH : \\MYMACHINE\root\wmi:WmiMonitorID.InstanceName="DISPLAY\\HWP3028\\4&23ad1ee&0&UID57547520_0" Active : True InstanceName : DISPLAY\HWP3028\4&23ad1ee&0&UID57547520_0 ManufacturerName : {72, 87, 80, 0...} ProductCodeID : {51, 48, 50, 56...} SerialNumberID : {54, 67, 77, 53...} UserFriendlyName : {72, 80, 32, 87...} UserFriendlyNameLength : 13 WeekOfManufacture : 19 YearOfManufacture : 2015 PSComputerName : MYMACHINE __GENUS : 2 __CLASS : WmiMonitorID __SUPERCLASS : MSMonitorClass __DYNASTY : MSMonitorClass __RELPATH : WmiMonitorID.InstanceName="DISPLAY\\HWP3027\\4&23ad1ee&0&UID59644672_0" __PROPERTY_COUNT : 9 __DERIVATION : {MSMonitorClass} __SERVER : MYMACHINE __NAMESPACE : root\wmi __PATH : \\MYMACHINE\root\wmi:WmiMonitorID.InstanceName="DISPLAY\\HWP3027\\4&23ad1ee&0&UID59644672_0" Active : True InstanceName : DISPLAY\HWP3027\4&23ad1ee&0&UID59644672_0 ManufacturerName : {72, 87, 80, 0...} ProductCodeID : {51, 48, 50, 55...} SerialNumberID : {54, 67, 77, 53...} UserFriendlyName : {72, 80, 32, 87...} UserFriendlyNameLength : 13 WeekOfManufacture : 19 YearOfManufacture : 2015 PSComputerName : MYMACHINE |
The Name etc. is in ASCII code so we have to take the numbers, convert to [char]
, and join them to get the name. Here’s what it looks like. I have to remove the 0 and concatenate the rest.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
PS C:\WINDOWS\system32> (Get-WmiObject WmiMonitorID -Namespace root\wmi).UserFriendlyName 72 80 32 87 50 51 55 49 100 0 0 0 0 72 80 32 87 50 51 55 49 100 0 0 0 0 |
So that’s what the first code snippet does. At the end of the pipeline I do a -join “;”
because my colleague wanted it semi-colon separated per monitor but that’s optional.