在 windows 10 中使用 powershell 在多个桌面上启动程序

Starting programs on multiple desktops using powershell in windows 10

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

这应该使用 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 和 NirCmd(同一网站)。 Monitorinfoview 工具不会检索多个桌面信息,而只会检索多个屏幕。 NirCmd 也不包含将程序发送到特定桌面的命令。

这应该会让您走上正确的路线。它使用 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

您可以从 https://inputsimulator.codeplex.com/ - link is dead but wayback machine should help https://web.archive.org/web/20210501220444/https://archive.codeplex.com/?p=inputsimulator

获取 C# InputSimulator.dll

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

calc Start-Sleep -Milliseconds 500

然后打开一个新的虚拟桌面[CSharpPS.PS]::NewVD() Start-Sleep -Milliseconds 500 notepad

您可以扩展 C# class 以允许在虚拟桌面之间切换或最小化所需的应用程序。

有适用于虚拟桌面的 Powershell 命令。 首先,您必须使用 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

您可能需要等待启动进程用 Start-Sleep

初始化其 window

在此处阅读更多内容:https://gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5