22

Suppose I want to start several programs C:\program1.exe, C:\program2.exe, etc. on multiple desktops in windows 10. For example, programs 1 and 2 should be started on desktop 1 side-by-side, program 3 should be started on the second desktop, while program 4 should be started minimized on the third desktop.

This should be achieved using either a powershell or batch script. The solution would be perfect if the powershell script automatically detects whether enough desktops are open and open more desktops if necessary.

batch-file-run-program-set position provides a solution to the problem of opening multiple programs side by side and resizing them. However, these solutions do not address multiple windows 10 desktops. The solutions rely on Monitorinfoview and NirCmd (same website). The tool Monitorinfoview does not retrieve multiple desktop information, but only multiple screens. NirCmd also does not include commands to send programs to specific desktops.

Community
  • 1
  • 1
HRSE
  • 323
  • 6
  • 13
  • 3
    Have you done any research about this ? This is a comminity, we dont build things for others, we sharr ou experience to help you if you are stuck doing somzthing that you started. – Chaibi Alaa Sep 10 '15 at 02:00
  • yes, i tried the following solution: [link](http://stackoverflow.com/questions/10392620/how-can-a-batch-file-run-a-program-and-set-the-position-and-size-of-the-window) However, I could not find out how powershell can target specific desktops. – HRSE Sep 10 '15 at 02:03
  • 2
    Then include what you received when doing that, errors, traces, details .. – Chaibi Alaa Sep 10 '15 at 02:04
  • 1
    @HREcon Right now there just isn't enough information about how to use virtual desktops programatically. There's some [vague interface documentation](https://msdn.microsoft.com/en-us/library/windows/desktop/mt186440%28v=vs.85%29.aspx) but it doesn't even say whether this is COM, or something else, or how to instantiate it. There are no examples for win32 even, never mind managed code, so at the moment there just doesn't seem to be any information about it. But besides that, Chaibi Alaa is right; this question isn't really appropriate for this site. – briantist Sep 10 '15 at 04:30
  • Thanks for the update. However, I don't quite understand why the question is not a good match for the site, given that the similar question [batch-file-run-program-set position](http://stackoverflow.com/questions/10392620/how-can-a-batch-file-run-a-program-and-set-the-position-and-size-of-the-window) was a good match? Many thanks! – HRSE Sep 13 '15 at 02:07
  • In 2012 the rules of this site were not so strict yet. – Thomas Weller Oct 08 '15 at 22:04
  • 1
    You currently can use Windows APIs to move a Window to another desktop. However, it will only work if your process owns that window. In your scenario it sounds like you are launching new processes and I do not think there is a way to move a window owned by another process. – MatthewG Oct 21 '15 at 21:11

2 Answers2

6

This should get you on the right lines. It uses PowerShell, C# (inside PS), Windows shortcuts and basic commands. Save this in a .ps1 script.

$Source = @"
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WindowsInput;

namespace CSharpPS
{
    public static class PS
    {
        public static void NewVD()
        {
            InputSimulator.SimulateKeyDown(VirtualKeyCode.LWIN);
            InputSimulator.SimulateKeyDown(VirtualKeyCode.CONTROL);
            InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_D);
            InputSimulator.SimulateKeyUp(VirtualKeyCode.LWIN);
            InputSimulator.SimulateKeyUp(VirtualKeyCode.CONTROL);
        }        
    }
}
"@;

Add-Type -TypeDefinition $Source -Language CSharp -ReferencedAssemblies InputSimulator.dll

You can get the C# InputSimulator.dll from https://inputsimulator.codeplex.com/

Once the type has been added you can then call [CSharpPS.PS]::NewVD() to create new virtual desktop. From here you can run specific programs. You may need to manually set a sleep also. An example :

calc Start-Sleep -Milliseconds 500

Then open a new virtual desktop [CSharpPS.PS]::NewVD() Start-Sleep -Milliseconds 500 notepad

You could expand the C# class to allow changing between Virtual Desktops or minimizing applications like you required.

Shaun Webb
  • 351
  • 3
  • 9
  • 5
    I believe this is the best answer possible and i believe this is ridiculous. Save us from ourselves, Microsoft. Would be great to allow apps to set their default desktop. – andymule Mar 03 '16 at 17:24
3

There are Powershell commands that work with virtual desktops. First, you will have to install virtual desktop module with Install-Module VirtualDesktop

# Switch to desktop 2 (count starts with 0)
Get-Desktop 1 | Switch-Desktop
    
# Move obs64.exe to desktop 3
(ps obs64)[0].MainWindowHandle | Move-Window (Get-Desktop 2) | Out-Null

You may have to wait for the starting process to initialize its window with Start-Sleep

Read more here: https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5

Josh Gallagher
  • 4,707
  • 2
  • 28
  • 53
Red Romanov
  • 324
  • 2
  • 10