Is there any way to check which monitor is the primary display, then execute a command based off of that in windows command line?

2.1k views Asked by At

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.

3

There are 3 answers

0
AUzun On BEST ANSWER

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.

0
I_Keep_Trying On
$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.

0
Paul Siersma On

Combine nircmd with AutoHotkey and you get this clean and simple script:

#Requires AutoHotkey v2.0

primary_num := MonitorGetPrimary()

if primary_num = 1
{
    RUN("C:\Apps\nircmd\nircmd.exe setprimarydisplay 2")
}
else
{
    RUN("C:\Apps\nircmd\nircmd.exe setprimarydisplay 1")
}