Question: I have 2 displays attached to my PC (one is my TV) running Windows 7 and I want to switch between them using a script. I know about the "displayswitch.exe" and its parameters (like /clone, /internal, etc). However, I need the script to work while the PC is locked. 

Pressing Windows+P works fine, while the system is locked, which also invokes displayswitch. However running a batch script with "displayswitch.exe /clone" does not work while the PC is locked.

To execute the script, I want to use the [Remote Launcher Application](http://owtroid.com/remotelauncher/mediawiki/index.php?title=Remote_Launcher) on my phone. The Remote Launcher works just fine with a script to shutdown my PC while it is locked, so it is in general able to execute scripts on the locked machine.

Is there any other way, to switch between my displays while the system is locked?

Answer: 
That's not a trivial task.

What displayswitch internally does is to call [SetDisplayConfig](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setdisplayconfig) function. this function must be invoked from a process which lives in a interactive console, otherwise it will return ERROR\_ACCESS\_DENIED

One may think that he can then invoke displayswitch from psexec using the -i option and indicating the currently active user session; usually powershell

```
(ps winlogon).si

```

returns all the interactive user sessions
but launched from psexec, displayswitch.exe doesn't works anyway. 

I suppose it is because in any case a command line application doesn't need the "graphical" infrastructure to run, and maybe some internal optimization happens in psexec to save to create a proper graphical environment (at least it doesn't work on my machine, i had no luck with the -x option either)

What you can do it is to write a very simple windows form program, it doesn't even need to create the actual form, it could simply invoke [SetDisplayConfig](https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setdisplayconfig) and die. But being a windows form application magically do the trick.

This way you can create a script that find the currently active interactive console id, and then use psexec like this (assuming 1 is the id of the interactive session)

```
psexec -accepteula -nobanner -i 1  C:\path-to-your-exe\your-exe.exe 

```

i had a powershell module loaded in my profile which i can call it from ssh or any remote connection and it switch my display even if my session is locked, or even if there is no user session logged at all.

obviously the user who runs the script must have the required grant to run psexec -i (mine is machine administrator, so it works, but i don't know which exact grant is required, you can create a functional machine administrator and invoke psexec passing these credential with -u -p parameters)
Try this:

* create a job in your windows scheduler executing the desired command (linke "displayswitch.exe /clone")
* set a user that has the permission to perform this command and save the password inside your new job
* don't set a trigger for the job but enable the option to start it manually
* use `schtasks /Run /S system /U username /P password /TN taskname` to trigger the job

This should execute the desired command no matter in which state your windows is as long as it is running and has network connection.