1

I'm using nircmd to change my primary display between my first and second monitor by using two batch files on my desktop; one to set primary display to the first monitor, and the other to set primary display to the second.

I was wondering if there was a way to check which display is the current primary display and then based off that result, change the display to the other one. In essence, I want to combine the two batch files into one so that I can switch displays with one file.

AUzun
  • 151
  • 1
  • 2
  • 9
  • 1
    Quick way - In your batch create a file when you switch to display 1, delete it when you switch to 2. IF EXISTS will tell you if it exists in which case you know #1 is the primary. – Alex K. Mar 10 '18 at 17:39
  • Fantastic work around, thanks! – AUzun Mar 10 '18 at 22:55

2 Answers2

2

Thanks to Alex K. If anyone else wanted to do something like this, here's what the code looks like (I'm sure there's a better way to do it).

I just created a folder that holds the empty text file that determines which monitor is the current primary display. FILEPATH is the path to that folder.

IF EXIST FILEPATH\test.txt ( nircmd.exe setprimarydisplay 2 cd c:\\ cd FILEPATH del test.txt ) ELSE ( nircmd.exe setprimarydisplay 1 cd c:\\ cd FILEPATH echo.> test.txt )

If anyone else stumbles upon this, feel free to ask for specifics.

AUzun
  • 151
  • 1
  • 2
  • 9
0
$flagFile = "N:\Apps_Portable\_CmdLineTools\nircmd-x64\MonSwitcher\monswitch.bin"

if (Test-Path $flagFile) {
    # Flag file exists, so switch to the other monitor
    & "N:\Apps_Portable\_CmdLineTools\nircmd-x64\nircmd.exe" setprimarydisplay 2
    Remove-Item $flagFile
} else {
    # Flag file does not exist, switch to the first monitor
    & "N:\Apps_Portable\_CmdLineTools\nircmd-x64\nircmd.exe" setprimarydisplay 1
    New-Item -ItemType File -Path $flagFile -Force
}

Using the logic you have for making a file to check which state we are in (rather than simply querying which monitor is primary right now because I don't see a way to do that), I have made this PS1/Powershell script. I then created a desktop shortcut to run powershell with this script.

I_Keep_Trying
  • 395
  • 3
  • 17