Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting programs on multiple desktops using powershell in windows 10

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.

like image 236
HRSE Avatar asked Sep 10 '15 01:09

HRSE


People also ask

How do I deploy a PowerShell script to multiple computers?

You can pass multiple computers to the -ComputerName parameter by separating their names with commas. If you also want to include the local computer, you add “localhost” or simply “.” to the list. By default, PowerShell will send the script immediately to all computers if 32 or fewer computer names are passed.

How do you switch between several open windows in the desktop environment?

Flip. You can use Flip to switch between open windows. To do this, press and hold the Alt key on your keyboard, then press the Tab key. Continue pressing the Tab key until the desired window is selected.


2 Answers

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

like image 155
Red Romanov Avatar answered Oct 06 '22 03:10

Red Romanov


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/ - link is dead but wayback machine should help https://web.archive.org/web/20210501220444/https://archive.codeplex.com/?p=inputsimulator

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.

like image 44
Shaun Webb Avatar answered Oct 06 '22 02:10

Shaun Webb