在Windows 10中使用PowerShell在多个桌面上启动程序

时间:2021-10-05 07:29:17

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.

假设我想在Windows 10中的多个桌面上启动多个程序C:\ program1.exe,C:\ program2.exe等。例如,程序1和2应该在桌面1上并排启动,程序3应该在第二个桌面上启动,而程序4应该在第三个桌面上最小化。

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.

这应该使用PowerShell或批处理脚本来实现。如果powershell脚本自动检测是否打开了足够的桌面并在必要时打开更多桌面,那么解决方案将是完美的。

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.

batch-file-run-program-set position为并排打开多个程序并调整它们的大小提供了解决方案。但是,这些解决方案不能解决多个Windows 10桌面问题。解决方案依赖于Monitorinfoview和NirCmd(同一网站)。 Monitorinfoview工具不会检索多个桌面信息,只能检索多个屏幕。 NirCmd也不包含将程序发送到特定桌面的命令。

1 个解决方案

#1


5  

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.

这应该让你走在正确的位置。它使用PowerShell,C#(内部PS),Windows快捷方式和基本命令。将其保存在.ps1脚本中。

$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/

您可以从https://inputsimulator.codeplex.com/获取C#InputSimulator.dll

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 :

添加类型后,您可以调用[CSharpPS.PS] :: NewVD()来创建新的虚拟桌面。从这里您可以运行特定的程序。您可能还需要手动设置睡眠。一个例子 :

calc Start-Sleep -Milliseconds 500

calc Start-Sleep -Milliseconds 500

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

然后打开一个新的虚拟桌面[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.

您可以扩展C#类以允许在虚拟桌面之间进行更改或最小化您所需的应用程序。

#1


5  

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.

这应该让你走在正确的位置。它使用PowerShell,C#(内部PS),Windows快捷方式和基本命令。将其保存在.ps1脚本中。

$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/

您可以从https://inputsimulator.codeplex.com/获取C#InputSimulator.dll

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 :

添加类型后,您可以调用[CSharpPS.PS] :: NewVD()来创建新的虚拟桌面。从这里您可以运行特定的程序。您可能还需要手动设置睡眠。一个例子 :

calc Start-Sleep -Milliseconds 500

calc Start-Sleep -Milliseconds 500

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

然后打开一个新的虚拟桌面[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.

您可以扩展C#类以允许在虚拟桌面之间进行更改或最小化您所需的应用程序。