Skip to content

WindowsDisplayManager Wiki

Patrick Fieldson edited this page Dec 7, 2023 · 3 revisions

About

WindowsDisplayManager is a PowerShell module for getting and setting Windows display settings.

It provides the ability to lookup and modify the resolution, refresh rate, and HDR enablement of any enabled monitor. It can also enable/disable video output to any monitor, and set any as the primary display. It can load/save/validate display states to file, and will handle the proper ordering of events to bulk update a set of monitors.

Additionally, it can report more detailed display information such as potential display source options, recommended resolution, color info, connection types, and more.

Installation

Install-Module -Name WindowsDisplayManager

See the PowerShell Gallery module listing here for more information.

Examples

Update the resolution to 4k@120Hz and enable HDR for the primary display. Print the result of the updates to console.

Import-Module WindowsDisplayManager
$primaryDisplay = WindowsDisplayManager\GetPrimaryDisplay
Write-Host "Update Successful?: $($primaryDisplay.SetResolution(3840,2160,120) -and $primaryDisplay.EnableHdr())"

Set the monitor with name "AW3423DWF" as primary, and disable the monitor with name "Internal Display". Print the result of the updates to console.

Import-Module WindowsDisplayManager
$alienwareMonitor = WindowsDisplayManager\GetDisplayByMonitorName -monitorName "AW3423DWF"
$internalLaptopMonitor = WindowsDisplayManager\GetDisplayByMonitorName -monitorName "Internal Display"
Write-Host "Update Successful?: $((WindowsDisplayManager\SetPrimaryDisplay -display $alienwareMonitor) -and $internalLaptopMonitor.Disable())"

Save the current display states to file, wait for 30 minutes, and then ensure the supported display settings are the same as they were when last saved (updating as necessary, and printing the update result to console).

Import-Module WindowsDisplayManager
$filePath = "C:\SomeFolder\SomeFile.json"
$enabledDisplays = WindowsDisplayManager\GetEnabledDisplays
WindowsDisplayManager\SaveDisplaysToFile -displays $enabledDisplays -filePath $filePath
Start-Sleep -seconds 60*30
Write-Host "Updates Successful?: $( WindowsDisplayManager\UpdateDisplaysFromFile -filePath $filePath -disableNotSpecifiedDisplays -validate )"

How It Works

At the core of WindowsDisplayManager is the Display PowerShell class, which represents a display source (ie one possible "channel" of video output from a graphics adapter) potentially mapped to a display target (ie a monitor or other connected graphics output device).

The Display class interacts with Windows via several Win32 C++ APIs (specifically DisplayConfig CCD APIs, and the more legacy DisplaySettings/DisplayDevices GDI APIs), passing first through WindowsDisplayManager's C# interoperability classes which allow PowerShell to interface with those APIs. Outside of source and target identifiers, class attributes are fetched real time via these means with each call to get them. To capture a snapshot of display attributes at any point in time, simply cast the Display object to a PSCustomObject with [PSCustomObject]$oneOfYourDisplays.

The main module file contains the logic for looking up and fetching such display objects, and the logic for multiple monitor operations such as setting primary (other multi monitor locations relative to one another have to be shuffled) or bulk loading a set of monitor settings.

Methods

GetAllPotentialDisplays

Returns a list of all potential display sources in the system (ie all output channels any graphics cards in the system can use). Useful for reporting or finding a display source to enable (ie connect to a monitor).

Args: None
Returns: [Display[]] Array of Display objects representing all enabled displays and potential video output sources

GetEnabledDisplays

Returns a list of all enabled displays in a multi monitor setup, or otherwise the (presumably) only enabled display.

Args: None
Returns: [Display[]] Array of Display objects representing all currently enabled displays

GetPrimaryDisplay

Gets the primary display in a multi monitor setup, or otherwise the only display.

Args: None
Returns: [Display] Primary display, or $null if for some reason it can't be found

GetDisplayByMonitorName

Gets the enabled display which represents (ie has a target pointing to) the supplied friendly monitor name, or $null if not found. Friendly monitor names can be found in Windows display settings- eg in Windows 10:

image

Args: [string]monitorName Friendly name of the display target aka output monitor
Returns: [Display] Display with a target which matches supplied friendly name, or $null if not found

GetRefreshedDisplay

Refreshes any target identifiers of the display based on that display's source. Useful for ensuring display object information is up to date after enabling or disabling a display (which is basically equivalent to adding or removing a target for a source).

Args: [Display]display Display object for which to refresh target identifiers
Returns: [Display]

SetPrimaryDisplay

Sets the specified enabled display as the primary display in a multi monitor setup.

Args: [Display]display Enabled Display object to set as the primary display
Returns: [boolean] True if successful or not applicable, False if an error occurred during the update

SaveDisplaysToFile

Saves the supplied display states to a specified file path (json format).

Args: [Display[]]displays Array of Display objects
      [string]filePath File path to save display states to (should be a .json file)
Returns: [boolean] True if successful, False if an error occurred

LoadDisplayStatesFromFile

Parses a json file of display states, and returns the contents as a set of display state objects.

Args: [string]filePath File path to load display states from
Returns: [Display[]] Array of Display objects parsed from the contents of the supplied file path

UpdateDisplaysFromFile

Bulk updates display settings according to a supplied json file of display states.

Args: [string]filePath File path to load display states from
      [switch]disableNotSpecifiedDisplays A flag of whether to disable any enabled displays that aren't present in the supplied states
      [switch]validate A flag of whether to double check after update that all updates occurred as expected, to handle false positives from Windows APIs
Returns: [boolean] True if successful or not applicable, False if an error occurred during the update

UpdateDisplaysToStates

Same as UpdateDisplaysFromFile, except takes a set of display states.

Args: [PSCustomObject[]]displayStates Array of PSCustomObjects representing copies of Display objects (ie their states at a point in time)
      [switch]disableNotSpecifiedDisplays A flag of whether to disable any enabled displays that aren't present in the supplied states
      [switch]validate A flag of whether to double check after update that all updates occurred as expected, to handle false positives from Windows APIs
Returns: [boolean] True if successful or not applicable, False if an error occurred during the update

CurrentDisplaysAreSameAsFile

Validates whether the current display settings are the same as the supplied json file of display states.

Args: [string]filePath
      [switch]validateAllEnabledDisplaysSpecified
Returns: [boolean] True if successful or not applicable, False if an error occurred during the update

CurrentDisplaysAreSameAsStates

Same as CurrentDisplaysAreSameAsFile, except takes a set of display object states.

Args: [string]filePath
      [switch]validateAllEnabledDisplaysSpecified
Returns: [boolean] True if successful or not applicable, False if an error occurred during the update

Classes

Display

Represents a single display source (one possible video "channel" of potential output) potentially outputting to a display target (ie a monitor or other connected graphics output device). These are the objects returned by the module display fetching functions (eg GetPrimaryDisplay).

Attributes

ID: Combination of display source id and target id if present
Description: Friendly combination of source and target descriptors
Active: Per Windows "whether a monitor is presented as being "on" by the respective GDI view"; Used to indicate a display source is actively outputting video signal to a target
Enabled: Whether the display is enabled- ie whether it has a connected target
Primary: Whether the display is primary in a multiple monitor setup. Will also be true if this is the only enabled display
Resolution:
    Width: Resolution width in pixels
    Height: Resolution height in pixels
    RefreshRate: Refresh rate in frames per second
HdrInfo:
    HdrSupported: Whether the display supports HDR
    HdrEnabled: Whether HDR is enabled per settings for the display
    BitDepth: Bits per color channel aka bit depth
Position: A coordinate pair representing a monitor's position relative to other monitors in a multi monitor setup.
    X: X coordinate
    Y: Y coordinate
RecommendedResolution: The resolution which is recommended for the output target monitor (doesn't include refresh rate)
    Width: Resolution width in pixels
    Height: Resolution height in pixels
Source: Output video signal source that identifies this display, ie an output channel for a graphics card
    Id: Unique source identifier, typically an index (eg 0)
    Name: Source name (eg "\\.\DISPLAY1")
    Description: Source description, usually graphics card name (eg "Intel(R) HD Graphics Family")
Target: Output target (eg monitor) that the display source is outputting to, also helps identify this display. Empty if display not enabled
    Id: Unique target identifier
    AdapterId: Identifier of unique display adapter outputting to this target
    FriendlyName: Friendly name of the connected target
    ConnectionType: Output technology type for the connection (eg HDMI, Displayport, etc)

Methods

Equals

Whether this display is the same (per source and target identifiers) as another

    Args: [Display]display The display to compare this display to
    Returns: [boolean] Whether this display is the same as the supplied display

ToTableString

Formats the display as a neat table string, particularly useful for logging

    Args: None
    Returns: [string] Output table formatted string

ToJsonString

Formats the display as a string in json format, useful for writing to file

    Args: None
    Returns: [string] Output json formatted string

Disable

Disables this display, ie disconnects it from its target. Note this won't automatically update target id information

    Args: None
    Returns: [boolean] True if successful or not applicable, False if an error occurred during the update

Enable

Reenables this display, ie reconnects it to it's target

    Args: None (assumes the monitor has a saved target id to use as a destination ie it was enabled when it was fetched)
    Returns: [boolean] True if successful or not applicable, False if an error occurred during the update

Enable

Enables this display, ie connects it to a supplied target

    Args: [int]destinationTargetId The ID of the target to connect to when enabling the display source
    Returns: [boolean] True if successful or not applicable, False if an error occurred during the update

SetResolution

Set display resolution including a refresh rate

    Args: 
        [int]width Resolution width in pixels
        [int]height Resolution height in pixels
        [int]refreshRate Refresh rate in frames per second
    Returns: [boolean] True if successful or not applicable, False if an error occurred during the update

SetResolution

Set display resolution without updating refresh rate

    Args:
        [int]width Resolution width in pixels
        [int]height Resolution height in pixels
    Returns: [boolean] True if successful or not applicable, False if an error occurred during the update

SetToRecommendedResolution

Set display resolution to the recommended width and height (refresh rate will not be updated)

    Args: None
    Returns: [boolean] True if successful or not applicable, False if an error occurred during the update

EnableHdr

Enable HDR for the display

    Args: None
    Returns: [boolean] True if successful or not applicable, False if an error occurred during the update

DisableHdr

Disable HDR for the display

    Args: None
    Returns: [boolean] True if successful or not applicable, False if an error occurred during the update

Compatibility

WindowsDisplayManager has been tested in both Windows 10 and 11, but per Windows API documentation should work in earlier versions of Windows back to Windows 7 as well.

See Also

SunshineAutomationSuite: My main example usage of this module (the reason I developed it). Useful if you want more examples of usage. If you are interested in game streaming, you may even find the app itself valuable!