Skip to content

Instantly share code, notes, and snippets.

@LawrenceHwang
Created February 18, 2022 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LawrenceHwang/1e17bff5843ce2be4f4254d452738af1 to your computer and use it in GitHub Desktop.
Save LawrenceHwang/1e17bff5843ce2be4f4254d452738af1 to your computer and use it in GitHub Desktop.
Set-MonitorInputSource - wraps around the ControlMyMonitor.exe
function Set-MonitorInputSource {
<#
.SYNOPSIS
Helper function to set the monitor state.
.EXAMPLE Switch to HDMI
Set-MonitorInputSource -InputSource HDMI
.EXAMPLE Switch to DisplayPort
Set-MonitorInputSource -InputSource DisplayPort
.EXAMPLE Switch to DisplayPort for monitor 1 and monitor 2
Set-MonitorInputSource -InputSource DisplayPort -Monitor 1
Start-Sleep -Milliseconds 300
Set-MonitorInputSource -InputSource DisplayPort -Monitor 2
.NOTES
# https://www.nirsoft.net/utils/control_my_monitor.html
# https://johnstonsoftwaresolutions.com/2019/02/09/tip-switch-monitor-inputs-with-command-line/
#>
[CmdletBinding()]
param(
[parameter(Mandatory = $true)]
[ValidateSet('HDMI', 'DisplayPort')]
[string]$InputSource,
[ValidateSet('1', '2')]
[string]$Monitor = '1',
[string]$ExePath = "$env:AppData\ControlMyMonitor.exe"
)
if (Test-Path $ExePath) {
$monitorHash = @{
'1' = @{
Name = '\\.\DISPLAY1\Monitor0'
InputSourceHash = @{
'DisplayPort' = 15
'HDMI' = 17
}
}
'2' = @{
Name = '\\.\DISPLAY2\Monitor0'
InputSourceHash = @{
'DisplayPort' = 15
'HDMI' = 17
}
}
}
[string]$ExePath = "$env:AppData\ControlMyMonitor.exe"
$argList = @(
'/SetValueIfNeeded'
$monitorHash[$Monitor].Name
'60'
$monitorHash[$Monitor].InputSourceHash[$InputSource]
)
Start-Process -FilePath $ExePath -ArgumentList $argList -Wait
} else {
Write-Warning "Unable to locate ControlMyMonitor.exe at: [$ExePath]"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment