简体   繁体   English

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

[英]How to Start a Universal Windows App (UWP) from PowerShell in Windows 10?

In PowerShell, if I run Get-AppxPackage , I get a list of UWP apps installed, including mine.在 PowerShell 中,如果我运行Get-AppxPackage ,我将获得已安装的UWP 应用程序列表,包括我的应用程序。 For example:例如:

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

Now I want to start this app.现在我想启动这个应用程序。

How to do this in PowerShell , or in cmd ?如何在PowerShellcmd中执行此操作?

During development, I've encountered a situation where the app family name occasionally changed.在开发过程中,我遇到过应用系列名称偶尔会更改的情况。 You can reliably launch an app by name with a simple lookup:您可以通过简单的查找按名称可靠地启动应用程序:

  • 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

With the Windows 10 Fall Creators Update 1709 (build 16299) you now have the ability to define an app execution alias for your UWP app, so you can launch it easily from cmd or powershell:借助 Windows 10 Fall Creators Update 1709(内部版本 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>

Furthermore, we now support commandline arguments for UWP apps.此外,我们现在支持 UWP 应用的命令行参数。 You can read them from the OnActivated event:您可以从 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;

See blog post: https://blogs.windows.com/buildingapps/2017/07/05/command-line-activation-universal-windows-apps/见博文: https : //blogs.windows.com/buildingapps/2017/07/05/command-line-activation-universal-windows-apps/

在 PowerShell 中试试这个:

start shell:AppsFolder\TonyHenrique.tonyuwpteste_h3h3tmhvy8gfc!App

如果您知道显示名称,则可以使用包含正确后缀的Get-StartApps

start "shell:AppsFolder\$(Get-StartApps "Groove Music" | select -ExpandProperty AppId)"

I know it's an old post, but I built a function to do it.我知道这是一个旧帖子,但我构建了一个 function 来完成它。
Hope it helps other folks.希望它能帮助其他人。

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"
        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM