如何在Windows 10中使用PowerShell启动通用Windows应用程序(UWP)?

9
在PowerShell中,如果我运行 Get-AppxPackage,我会得到一个列表,其中包括安装的UWP应用程序,包括我的应用程序。 例如:
Name              : TonyHenrique.tonyuwpteste
Publisher         : CN=tTony
Architecture      : X64
ResourceId        :
Version           : 1.1.12.0
PackageFullName   : TonyHenrique.tonyuwpteste_1.1.12.0_x64__h3h3tmhvy8gfc
InstallLocation   : C:\Program Files\WindowsApps\TonyHenrique.tonyuwpteste_1.1.12.0_x64__h3h3tmhvy8gfc
IsFramework       : False
PackageFamilyName : TonyHenrique.tonyuwpteste_h3h3tmhvy8gfc
PublisherId       : h3h3tmhvy8gfc
IsResourcePackage : False
IsBundle          : False
IsDevelopmentMode : False
Dependencies      : {Microsoft.NET.CoreRuntime.2.1_2.1.25801.2_x64__8wekyb3d8bbwe, Microsoft.VCLibs.140.00.Debug_14.0.25805.1_x64__8wekyb3d8bbwe,
                    TonyHenrique.tonyuwpteste_1.1.12.0_neutral_split.scale-100_h3h3tmhvy8gfc}
IsPartiallyStaged : False
SignatureKind     : Developer
Status            : Ok

现在我想启动这个应用程序。

如何在PowerShellcmd中执行此操作?


1
{btsdaf} - guiwhatsthat
5个回答

9
在开发过程中,我遇到了一个情况,即应用程序的名称偶尔会更改。您可以通过简单的查找可靠地启动应用程序名称:
  • Cmd

    powershell.exe explorer.exe shell:AppsFolder\$(get-appxpackage -name YourAppName ^| select -expandproperty PackageFamilyName)!App
    
  • Powershell

    explorer.exe shell:AppsFolder\$(get-appxpackage -name YourAppName | select -expandproperty PackageFamilyName)!App
    

2
这个方法适用于你自己的应用程序或特定情况,但不能普遍使用,因为没有给出!App。例如:Microsoft.BingNews_8wekyb3d8bbwe!AppexNews。 - papo
2
看起来在AppxManifest.xml文件中可以找到与应用程序标签相关的!Appx或!AppxNews后缀: <Application Id="AppexNews" Executable="Microsoft.Msn.News.exe" EntryPoint="Microsoft.Msn.News.App"> - Rob Meyer

8
如果您知道显示名称,可以使用Get-StartApps命令,它会包含正确的后缀:
start "shell:AppsFolder\$(Get-StartApps "Groove Music" | select -ExpandProperty AppId)"

8

随着Windows 10秋季创作者更新(版本号16299),您现在可以为您的UWP应用程序定义应用程序执行别名,以便您可以轻松地从cmd或powershell启动它:

<Extensions>
    <uap5:Extension
      Category="windows.appExecutionAlias"
      StartPage="index.html">
      <uap5:AppExecutionAlias>
        <uap5:ExecutionAlias Alias="MyApp.exe" />
      </uap5:AppExecutionAlias>
    </uap5:Extension>
</Extensions>

此外,我们现在支持UWP应用程序的命令行参数。您可以从OnActivated事件中读取它们:
async protected override void OnActivated(IActivatedEventArgs args)
{
    switch (args.Kind)
    {
        case ActivationKind.CommandLineLaunch:
            CommandLineActivatedEventArgs cmdLineArgs = 
                args as CommandLineActivatedEventArgs;
            CommandLineActivationOperation operation = cmdLineArgs.Operation;
            string cmdLineString = operation.Arguments;
            string activationPath = operation.CurrentDirectoryPath;

请看博客文章:https://blogs.windows.com/buildingapps/2017/07/05/command-line-activation-universal-windows-apps/,此文讨论了通用 Windows 应用程序的命令行激活。

我在这里发布了一个类似的问题,目前还没有解决方案。我想知道你是否有任何解决方案/想法?谢谢。 - nam
有没有可能从PowerShell关闭UWP应用程序,因为我已经尝试过了,但它不起作用;同时使用“-passthrough”参数启动进程也没有返回任何内容。 - Tims

6

在 PowerShell 中尝试以下操作:

start shell:AppsFolder\TonyHenrique.tonyuwpteste_h3h3tmhvy8gfc!App

根据你的答案,我最后使用了:explorer.exe shell:AppsFolder\TonyHenrique.tonyuwpteste_h3h3tmhvy8gfc!App - Tony

0

我知道这是一个旧帖子,但我编写了一个函数来完成它。
希望能帮助其他人。

function Start-UniversalWindowsApp {

    [CmdletBinding()]
    param (
        [Parameter(
            Mandatory,
            Position = 0,
            ValueFromPipeline,
            ValueFromPipelineByPropertyName = $false
        )]
        [ValidateNotNullOrEmpty()]
        [string]$Name,
        
        [Parameter()]
        [pscredential]$Credential
    )

    $queriedapp = $Global:UwpList | Where-Object { $PSItem.Name -like "*$Name*" }
    if (!$queriedapp) { Write-Error -Exception [System.Data.ObjectNotFoundException] -Message "No app was found with the name '$Name'." }
    if ($queriedapp.Count -gt 1) {
        $indexapplist = @()
        for ($i = 1; $i -le $queriedapp.Count; $i++) {
            $indexapplist += [pscustomobject]@{ Index = $i; App = $queriedapp[$i - 1] }
        }

        Write-Host @"
More than one app was found for the name $($Name):

"@

        $indexapplist | ForEach-Object { Write-Host "$($PSItem.Index) - $($PSItem.App.Name)" }
        $usrinput = Read-Host @"

Select one or all packages.
[I] Package Index  [A] All  [C] Cancel
"@

        while (($usrinput -ne 'A') -and ($usrinput -ne 'C') -and ($usrinput -notin $indexapplist.Index) ) {
            if ($usrinput) {
                Write-Host "Invalid option '$usrinput'."
            }
        }

        $appstorun = $null
        switch ($usrinput) {
            'A' { $appstorun = $queriedapp }
            'C' { $Global:LASTEXITCODE = 1223; return }
            Default { $appstorun = ($indexapplist | Where-Object { $PSItem.Index -eq $usrinput }).App }
        }

    }
    else {
        $appstorun = $queriedapp
    }

    if ($Credential) {
        foreach ($app in $appstorun) {
            Start-Process -FilePath 'explorer.exe' -ArgumentList "shell:AppsFolder\$($app.PackageFamilyName)!App" -Credential $Credential
        }
    }
    else {
        foreach ($app in $appstorun) {
            Start-Process -FilePath 'explorer.exe' -ArgumentList "shell:AppsFolder\$($app.PackageFamilyName)!App"
        }
    }

}

你知道如何在之后关闭它吗? - Tims

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接