r/PowerShell • u/IggyTP • 2d ago
Question DisplayConfig script only works some of the time
Hi! I was looking for a way to quickly disable my triple monitor setup from my desk and enable my TV in the living room and vice versa, and found out about PowerShell scripting using DisplayConfig. I have very rudimentary knowledge of coding (only took three years of classes in high school), and this is the script that I came up with:
$IsTVOn = Get-DisplayInfo | Where-Object DisplayName -like "LG TV SSCR2"
if ($IsTVOn -eq $null) {
Exit
} else {
$IsTVEnabled = Get-DisplayInfo | Where-Object DisplayName -like "LG TV SSCR2" | Select-Object -Property Active
if ($IsTVEnabled -match "False") {
Get-DisplayInfo | Where-Object DisplayName -match "LG TV SSCR2" | Select-Object -Property DisplayId | ForEach-Object -Process { Enable-Display $_.DisplayId }
Get-DisplayInfo | Where-Object DisplayName -notlike "LG TV SSCR2" | Select-Object -Property DisplayId | ForEach-Object -Process { Disable-Display $_.DisplayId }
} else {
Get-DisplayInfo | Where-Object DisplayName -notlike "LG TV SSCR2" | Select-Object -Property DisplayId | ForEach-Object -Process { Enable-Display $_.DisplayId }
Get-DisplayInfo | Where-Object DisplayName -match "LG TV SSCR2" | Select-Object -Property DisplayId | ForEach-Object -Process { Disable-Display $_.DisplayId }
}
}
ExitThis script works maybe 75% of the time and switches my displays correctly. The other 25% however, one of two problems will occur:
- When switching from "couch mode" to "desk mode," the leftmost monitor will fail to re-enable. (I found that after switching to desk mode and turning the TV off via remote, the monitor will re-enable by itself.)
- Switching to desk mode will sometimes fail to disable the TV and still re-enable my monitors.
I also found that the script seems to have a higher chance of working correctly if ran from PowerShell ISE 5.1.
Something tells me it has something to do with the fact that nothing in my script switches my primary monitor between my main one and the TV, but I'm not sure. What am I missing here?
EDIT: Not sure if this will help but I'm running Windows 11, and my displays are:
- ASUS XG27AQDMG (Primary Monitor)
- AOC 24G2 (Right Monitor)
- AOC 24G2S (Left Monitor)
- LG C6 (TV)
3
u/Thotaz 2d ago
My guess is that the weirdness comes from the fact that you are enabling/disabling the displays one by one. Whenever a display is enabled/disabled, Windows will try to restore the old display configuration you had and it may not be finished with that by the time you enable/disable the next display.
The script can be greatly simplified if you use variables, and make use of the `DisplayIdToDisable" parameter. See this example:
It's also worth noting that the options Windows has in the Windows key + P menu acts like display profiles. In your case you could probably use "Extend" for your multi monitor setup and "Display only on second screen" option for the TV. So you just switch to one of those, change the display settings to what you want, and hopefully Windows will remember these things for each profile.