You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不切换桌面的情况下通过PowerShell在虚拟桌面启动应用

在不切换桌面的情况下在指定虚拟桌面启动进程

当前脚本通过Switch-Desktop切换到新桌面再启动程序,会打断用户当前工作。因为Start-Process没有直接指定虚拟桌面的参数,我们可以通过调用Windows虚拟桌面API,在进程启动后将其窗口移动到目标虚拟桌面,全程无需切换桌面。

具体实现步骤

1. 定义Windows虚拟桌面API接口

先在PowerShell中添加必要的类型定义,用于调用系统的虚拟桌面管理功能:

Add-Type @"
using System;
using System.Runtime.InteropServices;

[ComImport]
[Guid("a5cd92ff-29be-454c-8d04-d82879fb3f1b")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IVirtualDesktopManager {
    bool IsWindowOnCurrentVirtualDesktop(IntPtr hwnd);
    Guid GetWindowDesktopId(IntPtr hwnd);
    void MoveWindowToDesktop(IntPtr hwnd, ref Guid desktopId);
}

[ComImport]
[Guid("aa509086-5ca9-4c25-8f95-589d3c07b48a")]
public class VirtualDesktopManager {
}
"@

2. 编写进程移动函数

创建一个函数,将指定进程的窗口移动到目标虚拟桌面:

function Move-ProcessToVirtualDesktop {
    param(
        [Parameter(Mandatory=$true)]
        [System.Diagnostics.Process]$Process,
        [Parameter(Mandatory=$true)]
        [Guid]$DesktopId
    )

    $vdm = New-Object VirtualDesktopManager
    $hwnd = $Process.MainWindowHandle
    
    # 有些进程启动后窗口不会立即创建,短暂等待后重试
    if ($hwnd -eq [IntPtr]::Zero) {
        Start-Sleep -Milliseconds 500
        $hwnd = $Process.MainWindowHandle
    }

    if ($hwnd -ne [IntPtr]::Zero) {
        $vdm.MoveWindowToDesktop($hwnd, [ref]$DesktopId)
    }
}

3. 修改原有脚本

去掉Switch-Desktop的切换逻辑,改为启动进程后直接移动到目标虚拟桌面:

$file1 = ".\configstore\config1.txt"
$filec = ".\config.txt"

$result1 = ".\results\applaunchresult1.txt"

$freq1 = 15
$iterations = 3

# (这里粘贴前面的Add-Type和Move-ProcessToVirtualDesktop代码)

function MeasureAppLaunchTime {

    "Number of virtual desktops: $(Get-DesktopCount)"

    Start-Sleep -Seconds 1

    "Create a new desktop:"
    $NewDesktop = New-Desktop
    $OldDesktop = Get-CurrentDesktop
    # 假设你的New-Desktop返回的对象包含Id属性,或者实现Get-DesktopId函数获取虚拟桌面的Guid
    $newDesktopId = Get-DesktopId $NewDesktop

    "Number of virtual desktops: $(Get-DesktopCount)"
    "Number of new desktop: $(Get-DesktopIndex($NewDesktop))"
    "Name of new desktop: $(Get-DesktopName($NewDesktop))"

    $index = 1

    do {
        echo "Running Iteration - $index, Total - $iterations" 

        Copy-Item $file1 -Destination $filec 
        # 启动AppTimer.exe,保留当前桌面焦点
        $process = Start-Process -FilePath .\AppTimer.exe -WindowStyle Hidden -PassThru
        # 将进程窗口移动到新虚拟桌面
        Move-ProcessToVirtualDesktop -Process $process -DesktopId $newDesktopId

        Start-Sleep -Seconds 10

        echo "sleeping for next $freq1 sec"
        Start-Sleep -Seconds $freq1

        $index++
    } while ($index -le $iterations)
}

补充说明

  • 如果使用第三方虚拟桌面管理模块(比如VirtualDesktop),获取虚拟桌面Id会更简单:
    # 先安装模块:Install-Module -Name VirtualDesktop -Scope CurrentUser
    $newDesktop = New-VirtualDesktop
    $newDesktopId = $newDesktop.Id
    
  • 部分进程可能需要更长的窗口创建等待时间,可根据实际情况调整Start-Sleep -Milliseconds的数值。
  • 此方法全程无需切换桌面,用户可以在当前桌面正常工作,目标进程在后台虚拟桌面运行。

内容的提问来源于stack exchange,提问作者suresh prep

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 11:15:53