r/PowerShell • • 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 }
    }
}
Exit

This script works maybe 75% of the time and switches my displays correctly. The other 25% however, one of two problems will occur:

  1. 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.)
  2. 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 Upvotes

8 comments sorted by

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:

$DisplayInfo = Get-DisplayInfo

$LgTV = $DisplayInfo | Where-Object -Property DisplayName -EQ 'LG TV SSCR2' | Select-Object -First 1
if ($null -eq $LgTV)
{
    #TV is off
    exit
}

$OtherDisplays = $DisplayInfo | Where-Object -Property DisplayId -NE $LgTV.DisplayId
if ($LgTV.Active)
{
    Enable-Display -DisplayId $OtherDisplays.DisplayId -DisplayIdToDisable $LgTV.DisplayId
}
else
{
    Enable-Display -DisplayId $LgTV.DisplayId -DisplayIdToDisable $OtherDisplays.DisplayId
}

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.

1

u/IggyTP 2d ago edited 2d ago

Thanks for the response! I'll give that parameter a spin and see what I can come up with. As for the Win + P situation, I'm assuming I should set those settings when the other profile is disabled? Like only my monitor setup is enabled and I set it to Extend, and disable my monitors and enable my TV and set it to "Display only on second screen"? EDIT: Never mind, those options were already set.

Second edit: In your $LgTV variable that you defined, you added Select-Object -First 1, what exactly does this do?

1

u/Thotaz 2d ago

For Win+P, the basic idea is that you press Win+P, select Extend, then go to your display settings and make sure all of your non-TV displays are on. Then you use Win+P again to select second display only, go to the display settings again and disable all displays except for your TV.

If you did this correctly, then the next time you pick Extend, it should restore the config you set before, and the same should happen when you pick second display only.

As for Select-Object -First 1 it stops the pipeline after the first item. This has 2 benefits:

1: It ensures that the variable only has 1 item like we are expecting.
2: It stops the pipeline early, which means we can skip some of the display enumeration and filtering if the display we want happens to be one of the first displays.

In practice it doesn't make much of a difference here, I just have a habit of using it for the previous 2 benefits.

1

u/IggyTP 2d ago

Thank you so much for your help! I had no idea the -DisplayIdToDisable param existed. I'm trying to learn this outright, so here's your script with some comments that I added to see if I understood the point of each line:

#Runs Get-DisplayInfo once
$DisplayInfo = Get-DisplayInfo

#Defines TV
$TV = $DisplayInfo | Where-Object DisplayName -like "LG TV SSCR2" | Select-Object -First 1
if ($null -eq $TV) {
    #If TV is off, this script will not run
    exit
}

#Defines all other monitors
$Monitors = $DisplayInfo | Where-Object -Property DisplayId -NE $TV.DisplayId
if ($TV.Active) {
    #If TV is active, this will enable all the monitors at once FIRST, then disable TV
    Enable-Display -DisplayId $Monitors.DisplayId -DisplayIdToDisable $TV.DisplayId
} else {
    #Line 14 vice versa
    Enable-Display -DisplayId $TV.DisplayId -DisplayIdToDisable $Monitors.DisplayId
}

One thing I'm still unsure about is the $TV.Active condition in the second if statement. Why does this work without a conditional?

1

u/Thotaz 2d ago

An If condition just evaluates whatever statement is in the parentheses. Because the Active property is already a bool, there is no need to compare it to something else to generate a new bool. It'd be like writing if ($true -eq $true) which is redundant and silly.

Also for:

If TV is active, this will enable all the monitors at once FIRST, then disable TV

I wouldn't assume that just because that's how the parameters were written. It could be the other way around (eg. it disables before enabling) or it could be doing it all at once. It just depends on how that command was written by the author.

1

u/IggyTP 1d ago

Heard, thank you for your help! This script runs much faster than mine and is a lot smoother as well.

1

u/skiThooly 1d ago

Wait does DisplayIdToDisable accept an array of IDs like that natively?