Get Desktop Attached Monitor Information – PowerShell Script

Why we need this script

Sysadmins admins need to keep track of their hardware inventory. Most software deployment systems such as SCCM and PDQ Deploy keep track of the computer info and can generate nice reports. What about the monitors? What if you need to know the monitor model, size and even serial number?

There are third party tools that you can buy to get monitor info, but that can be pricey. That’s why you might want to take a look at this nifty PowerShell script.

How Does IT Work?

  • This script queries WMI for monitor info and displays it in PS.
  • It uses known vendor ’codes” to identify monitor types
  • There is a lot of cool formatting done that can be changed to fit your needs

Step-by-step guide

  1. Save the attached Powershell script to a folder on your workstation.
  2. Open Powershell and choose “Run as Administrator”
  3. Make sure your Execution Policy is set to Bypass. You can run Get-ExecutionPolicy to check you settings

C:\b70844ee19e6a1df28730feb9120d47c

The script can be used with single computer names/ip or multiple computer names that can be imported from a csv. To import computers from csv, set the $Computers parameter with the computers list.

    $computers = Get-Content -Path C:/(Path to CSV). The csv needs to have one computer per line.

4.Run the script :

PS C:/> Get-Monitor.ps1 -ComputerName $Computers

Or

PS C:/> Get-Monitor.ps1 -ComputerName ComputerName

5.If youre querying multiple computers you can export the results to a csv by adding the | Export-CSV – Path (Path) eg

#> .\Get-Monitor.ps1 -ComputerName $Computers | Export-Csv -Path C:\(Location)Results.txt

The results will look like this:

  • Computers with multiple monitors will show up more than once in the “AttachedComputer” Collumn
  • Laptops attached to multiple monitors will show the attached monitor as well as the laptop inbuilt monitor as well. The inbuilt laptop monitor have serial number set to “0” and a manufacturer name of “CMN”
  • The script is limited to machines that are online that the time its run.

NB: I used various online resources. I did not write this script in its entirety.

 

Here is the script:

 

<#

.SYNOPSIS
This powershell function gets information about the monitors attached to any computer. It uses EDID information provided by WMI. If this value is not specified it pulls the monitors of the computer that the script is being run on.

.DESCRIPTION
The function begins by looping through each computer specified. For each computer it gets a list of monitors.
It then gets all of the necessary data from each monitor object and converts and cleans the data and places it in a custom PSObject. It then adds
the data to an array. At the end the array is displayed.

.PARAMETER ComputerName
Use this to specify the computer(s) which you’d like to retrieve information about monitors from.

.EXAMPLE
PS C:/> Get-Monitor.ps1 -ComputerName MyComputer

Manufacturer Model SerialNumber AttachedComputer
———— —– ———— —————
Acer Acer K272HUL T0SfADAFD MyComputer

.EXAMPLE
PS C:/> $Computers = @(“Comp1″,”Comp2″,”Comp3”)
PS C:/> Get-Monitor.ps1 -ComputerName $Computers

Manufacturer Model SerialNumber AttachedComputer
———— —– ———— —————-

#>

[CmdletBinding()]
PARAM (
[Parameter(ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
[String[]]$ComputerName = $env:ComputerName
)

#List of Manufacture Codes that could be pulled from WMI and their respective full names. Used for translating later down.
$ManufacturerHash = @{
“AAC” = “AcerView”;
“ACR” = “Acer”;
“APP” = “Apple Computer”;
“AUO” = “Asus”;
“CMO” = “Acer”;
“CPQ” = “Compaq”;
“DEL” = “Dell”;
“HWP” = “HP”;
“LEN” = “Lenovo”;
“SAN” = “Samsung”;
“SAM” = “Samsung”;
“SNY” = “Sony”;
“SRC” = “Shamrock”;
“SUN” = “Sun Microsystems”;
“SEC” = “Hewlett-Packard”;
“TAT” = “Tatung”;
“TOS” = “Toshiba”;
“TSB” = “Toshiba”;
“VSC” = “ViewSonic”;
“UNK” = “Unknown”;
“_YV” = “Fujitsu”;
}

#Takes each computer specified and runs the following code:
ForEach ($Computer in $ComputerName) {

#Grabs the Monitor objects from WMI
$Monitors = Get-WmiObject -Namespace “root\WMI” -Class “WMIMonitorID” -ComputerName $Computer -ErrorAction SilentlyContinue

#Creates an empty array to hold the data
$Monitor_Array = @()

#Takes each monitor object found and runs the following code:
ForEach ($Monitor in $Monitors) {

#Grabs respective data and converts it from ASCII encoding and removes any trailing ASCII null values
If ([System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName) -ne $null) {
$Mon_Model = ([System.Text.Encoding]::ASCII.GetString($Monitor.UserFriendlyName)).Replace(“$([char]0x0000)”,””)
} else {
$Mon_Model = $null
}
$Mon_Serial_Number = ([System.Text.Encoding]::ASCII.GetString($Monitor.SerialNumberID)).Replace(“$([char]0x0000)”,””)
$Mon_Attached_Computer = ($Monitor.PSComputerName).Replace(“$([char]0x0000)”,””)
$Mon_Manufacturer = ([System.Text.Encoding]::ASCII.GetString($Monitor.ManufacturerName)).Replace(“$([char]0x0000)”,””)

#Sets a friendly name based on the hash table above. If no entry found sets it to the original 3 character code
$Mon_Manufacturer_Friendly = $ManufacturerHash.$Mon_Manufacturer
If ($Mon_Manufacturer_Friendly -eq $null) {
$Mon_Manufacturer_Friendly = $Mon_Manufacturer
}

#Creates a custom monitor object and fills it with 4 NoteProperty members and the respective data
$Monitor_Obj = [PSCustomObject]@{
Manufacturer = $Mon_Manufacturer_Friendly
Model = $Mon_Model
SerialNumber = $Mon_Serial_Number
AttachedComputer = $Mon_Attached_Computer
}

#Appends the object to the array
$Monitor_Array += $Monitor_Obj

} #End ForEach Monitor

#Outputs the Array
$Monitor_Array

} #End ForEach Computer