如何使用 powershell 为 Windows 10 通用应用程序创建桌面快捷方式?

Pet*_*te 5 powershell uwp

我有一个我创建的 UWP 应用程序,想使用 powershell 在桌面上创建一个快捷方式。

为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()
Run Code Online (Sandbox Code Playgroud)

但是我正在为通用应用程序的目标使用什么而苦苦挣扎。

Gra*_*eng 7

为 UWP 应用创建快捷方式与经典桌面不同。您可以参考我的另一个答案Wherelinked UWP tile?

要使用 powershell 在桌面上创建 UWP 应用程序的快捷方式,您可以使用如下代码:

$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()
Run Code Online (Sandbox Code Playgroud)

您可以使用@TessellatingHeckler 提供的链接中的方法找到 AppUserModelId,并将Microsoft.SDKSamples.AdventureWorks.CS_8wekyb3d8bbwe!App代码中的替换为您想要的 AppUserModelId。

  • 这更快,似乎在 W10 中工作:Windows 键 + R,输入 `shell:AppsFolder`,搜索 UWP 应用程序,右键单击并拖动到桌面 -> 创建快捷方式 (4认同)

小智 6

防止您的快捷方式具有标准资源管理器图标。$Shortcut.TargetPath像这样改变:

$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()
Run Code Online (Sandbox Code Playgroud)

这样,快捷方式将与应用程序具有相同的图标。

您还可以*.url通过 URI创建快捷方式(如果相关应用程序支持)(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()
Run Code Online (Sandbox Code Playgroud)