8

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?

Pete
  • 105
  • 1
  • 1
  • 5
  • For starters, it looks like your `$TargetFile` doesn't include a drive letter. That could be your own attempt to obfuscate, however. What, specifically, do you mean you're _struggling with what to use_? – gravity Jul 13 '16 at 18:52
  • 1
    I Googled for shortcut targets for UWP apps and I found this http://winaero.com/blog/exclusive-how-to-start-a-modern-app-from-desktop-without-going-to-the-metro-start-screen/ – TessellatingHeckler Jul 13 '16 at 19:04
  • The $TargetFile path was just pseudo code.. Grace Feng had the answer – Pete Jul 15 '16 at 01:52

4 Answers4

9

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.

Community
  • 1
  • 1
Grace Feng
  • 16,564
  • 2
  • 22
  • 45
  • 4
    This is faster and seems to work in W10: Windows key + R, type in `shell:AppsFolder`, search for the UWP app, right click and drag to desktop -> Create shortcut – Tom Feb 16 '18 at 18:02
  • @tom 30 years of Windows, and it's the 1st time I see this! :) – not2qubit Jan 08 '20 at 22:02
  • I am not able to see the "AppUserModelId" in windows 10. there is no view options for the AppsFolder – Ali123 Jan 12 '20 at 11:41
7

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://learn.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()
ShEp
  • 87
  • 1
  • 5
  • I expanded the answer to include documentation to determine the target path. Feel free to also add the $Shortcut.Arguments if needed from the other answer. – Kevin Feb 05 '19 at 23:29
1

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.

Kevin
  • 2,761
  • 1
  • 27
  • 31
  • Will the packageFamilyName change per device or is it the same for all devices that the app has been installed on? – Ali123 Jan 12 '20 at 11:51
  • I only have one Windows device, would need somebody else to confirm – Kevin Jan 13 '20 at 18:08
1

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

Garric
  • 591
  • 3
  • 10