简体   繁体   中英

How can I create a desktop shortcut for a Windows 10 Universal app using powershell?

I have a UWP app I created and want to use powershell to create a shortcut on the desktop.

Creating a shortcut is easy for an exe

$TargetFile =  "\Path\To\MyProgram.exe"
$ShortcutFile = "$env:USERPROFILE\Desktop\MyShortcut.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetFile
$Shortcut.Save()

But I'm struggling with what to use as the Target for the Universal apps.. I also know I can easily create a shortcut to an app manually but for this purpose, it needs to be done with PowerShell.. any ideas?

Creating shortcut for UWP app is a different story from classic desktop. You can refer to my another answer Where linked UWP tile?

To create a shortcut of an UWP app on the desktop using powershell, you can for example code like this:

$TargetFile =  "C:\Windows\explorer.exe"
$ShortcutFile = "$env:USERPROFILE\Desktop\MyShortcut.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.Arguments="shell:AppsFolder\Microsoft.SDKSamples.AdventureWorks.CS_8wekyb3d8bbwe!App"
$Shortcut.TargetPath = $TargetFile
$Shortcut.Save()

You can find the AppUserModelId using the method in the link provided by @TessellatingHeckler, and replace the Microsoft.SDKSamples.AdventureWorks.CS_8wekyb3d8bbwe!App in the code with your desired AppUserModelId.

To prevent your shortcut from having the standard Explorer icon. Change the $Shortcut.TargetPath like this :

$TargetPath =  "shell:AppsFolder\Microsoft.Windows.Cortana_cw5n1h2txyewy!CortanaUi"
$ShortcutFile = "$Home\Desktop\Cortana.lnk"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetPath
$Shortcut.Save()

This way the shortcut will have the same icon as the application.

You can also create a shortcut *.url via URI (if the application in question supports it) ( https://docs.microsoft.com/en-us/windows/uwp/launch-resume/launch-default-app ) :

$TargetPath =  "ms-cortana:"
$ShortcutFile = "$Home\Desktop\Cortana.url"
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
$Shortcut.TargetPath = $TargetPath
$Shortcut.Save()

To determine the correct TargetPath https://www.tenforums.com/software-apps/57000-method-open-any-windows-10-apps-command-line.html documents the required steps:

List all applications via Powershell: get-appxpackage > %TEMP%\\application_list.txt

Open list: notepad %TEMP%\\application_list.txt

Find the PackageFamilyName for your app and navigate to the application's InstallLocation using Windows Explorer

Opening the AppxManifest.xml shows the Application ID: <Application Id="Microsoft.WinDbg.DbgSrv64" for the executable.

Combine the PackageFamilyName!ApplicationID to form your TargetPath

Microsoft.WinDbg_8wekyb3d8bbwe!Microsoft.WinDbg.DbgSrv64 for WinDbg Preview for instance.

I don't remember where I got this code from, but it's the best I know for finding a path ...

Powershell

$installedapps = get-AppxPackage
foreach ($app in $installedapps)
{
    foreach ($id in (Get-AppxPackageManifest $app).package.applications.application.id)
    {
        $line = $app.Name + " = " + $app.packagefamilyname + "!" + $id
        echo $line
    }
}

... and there is an addition for this How to avoid error when Get-AppxPackageManifest not found

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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