Private/Get-PrimaryMonitorResolution.ps1

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
function Get-PrimaryMonitorResolution {
    param (
        [Parameter(Mandatory = $true, Position = 1)]
        [Object[]]
        $Monitors
    )

    foreach ($Monitor in $Monitors) {
        $Item = Get-ItemProperty $Monitor.PSPath

        if ($Item.'Attach.ToDesktop') {
            #this is a monitor

            if (($Item.'Attach.RelativeX' -eq 0) -and ($Item.'Attach.RelativeY' -eq 0)) {
                #this is the primary monitor
                $Width = $Item.'DefaultSettings.XResolution'
                $Height = $Item.'DefaultSettings.YResolution'
            }
        }
    }

    if ((!$Width) -and (!$Height)) {
        Throw "Failed to find width and height"
    }

    return @{
        Width = $Width
        Height = $Height
    }
}