HRS*_*RSE 21 windows powershell virtual-desktop windows-10 powershell-5.0
假设我想在Windows 10中的多个桌面上启动多个程序C:\ program1.exe,C:\ program2.exe等.例如,程序1和2应该在桌面1上并排启动,程序3应该在第二个桌面上启动,而程序4应该在第三个桌面上最小化.
这应该使用PowerShell或批处理脚本来实现.如果powershell脚本自动检测是否有足够的桌面打开并在必要时打开更多桌面,那么解决方案将是完美的.
batch-file-run-program-set position为并排打开多个程序并调整它们的大小提供了解决方案.但是,这些解决方案不能解决多个Windows 10桌面问题.解决方案依赖于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
Run Code Online (Sandbox Code Playgroud)
您可以从https://inputsimulator.codeplex.com/获取C#InputSimulator.dll
添加类型后,您可以调用[CSharpPS.PS]::NewVD()以创建新的虚拟桌面.从这里您可以运行特定的程序.您可能还需要手动设置睡眠.一个例子 :
calc
Start-Sleep -Milliseconds 500
然后打开一个新的虚拟桌面 [CSharpPS.PS]::NewVD()
Start-Sleep -Milliseconds 500
notepad
您可以扩展C#类以允许在虚拟桌面之间进行更改或最小化您所需的应用程序.
有一些适用于虚拟桌面的 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
Run Code Online (Sandbox Code Playgroud)
您可能需要等待启动过程来初始化其窗口 Start-Sleep
在此处阅读更多信息:https : //gallery.technet.microsoft.com/scriptcenter/Powershell-commands-to-d0e79cc5