windows 10中如何使用powershell管理uwp app的启动项?

How to use powershell to manage startup items of uwp app in windwos 10?

我希望通过powershell enable/disable uwp app的启动项,但是我尝试使用gcim win32_startupcommandGet-Item HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run都找不到,shell:startup也是空的,好像uwp app是看不见的,只能在任务管理器->启动里面看到,怎么办?

我尝试用Regshot对比了启用启动项前后的区别,如下:

另外,我发现在UWP或者任务管理器启动之前,这些注册表项是不存在的,所以最后我认为这可能是一个非常复杂的解决方案,我放弃了这样做。

下面是注册表中 Skype 应用程序启动信息所在位置的示例。注册表项名称似乎不标准;也许您可以从清单或其他东西中获取密钥的名称。 Uwp 应用程序给管理员带来不便。甚至 Sysinternals Autoruns 也检测不到这些。

$a = Get-AppxPackage *skype* | Get-AppPackageManifest
$a.package.Applications.Application.Extensions.extension.startuptask.taskid
SkypeStartup

$a = get-appxpackage *windowsterminal* | Get-AppxPackageManifest
$a.package.Applications.Application.Extensions.extension.startuptask.taskid
StartTerminalOnLoginTask


# list all the app startups
$starts = get-appxpackage -pv app | get-appxpackagemanifest | % {
if ($_.package.Applications.Application.Extensions.extension.startuptask.taskid) {
  [pscustomobject] @{ PackageFamilyName = $app.PackageFamilyName
  TaskID = $_.package.Applications.Application.Extensions.extension.startuptask.taskid} } }
$starts

PackageFamilyName                       TaskID
-----------------                       ------
Microsoft.SkypeApp_kzf8qxf38zg5c        SkypeStartup
AppleInc.iTunes_nzyj5cx40ttqa           {AppleMobileDeviceProcess, iTunesHelper}
Microsoft.WindowsTerminal_8wekyb3d8bbwe StartTerminalOnLoginTask


# so maybe to disable startups by setting them to 1:
$starts | % {
  $fname = $_.PackageFamilyName
  $_.taskid | % {
    $taskid = $_
    Set-ItemProperty "HKCU:\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData$fname$taskid" State 1 -whatif
  }
}

What if: Performing the operation "Set Property" on target "Item: HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData\Microsoft.SkypeApp_kzf8qxf38zg5c\SkypeStartup Property: State".
What if: Performing the operation "Set Property" on target "Item: HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData\AppleInc.iTunes_nzyj5cx40ttqa\AppleMobileDeviceProcess Property: State".
What if: Performing the operation "Set Property" on target "Item: HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData\AppleInc.iTunes_nzyj5cx40ttqa\iTunesHelper Property: State".
What if: Performing the operation "Set Property" on target "Item: HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData\Microsoft.WindowsTerminal_8wekyb3d8bbwe\StartTerminalOnLoginTask Property: State".

2为启用,1为禁用。在“设置”应用中搜索“启动应用”。

Skype:

HKCU\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData\Microsoft.SkypeApp_kzf8qxf38zg5c\SkypeStartup\State=1(dword)(来自 procmon 类别的是写入)

Windows 航站楼:

HKCU\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData\Microsoft.WindowsTerminal_8wekyb3d8bbwe\StartTerminalOnLoginTask\State=1

iTunes 有 2 个启动任务:

HKCU\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData\AppleInc.iTunes_nzyj5cx40ttqa\AppleMobileDeviceProcess\State

HKCU\Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData\AppleInc.iTunes_nzyj5cx40ttqa\iTunesHelper\State

也许是这个? https://docs.microsoft.com/en-us/uwp/api/windows.applicationmodel.startuptask.disable?view=winrt-19041

https://docs.microsoft.com/en-us/uwp/api/windows.applicationmodel.startuptask?view=winrt-19041

[Windows.ApplicationModel.StartupTask]

如果有人告诉我如何用 C# 实现,我可以将它翻译成 Powershell。

这是一个潜在的 work-around,直到本机调用 [Windows.ApplicationModel.StartupTask]::RequestEnableSync()::Disable() 方法实现...

在此示例中,我正在切换 Skype 桌面扩展启动任务的状态:

$app = (Get-AppxPackage | Where-Object -Property Name -EQ -Value Microsoft.SkypeApp)
$pkgName = $app.PackageFamilyName
$startupTask = ($app | Get-AppxPackageManifest).Package.Applications.Application.Extensions.Extension | Where-Object -Property Category -Eq -Value windows.startupTask
$taskId = $startupTask.StartupTask.TaskId
$state = (Get-ItemProperty -Path "HKCU:Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData$pkgName$taskId" -Name State).State
$regKey = "HKCU:Software\Classes\Local Settings\Software\Microsoft\Windows\CurrentVersion\AppModel\SystemAppData$pkgName$taskId"
if ($state -in 0,1,3) {
    Set-ItemProperty -Path $regKey -Name UserEnabledStartupOnce -Value 1
    Set-ItemProperty -Path $regKey -Name State -Value 2
} else {
    $lastDisabled = [int](New-TimeSpan -Start (Get-Date '1970-01-01 00:00:00 GMT') -End (Get-Date)).TotalSeconds
    Set-ItemProperty -Path $regKey -Name LastDisabledTime -Value $lastDisabled
    Set-ItemProperty -Path $regKey -Name State -Value 1
}

有关可能的 State 值的详细信息,请参阅:https://docs.microsoft.com/en-us/uwp/api/windows.applicationmodel.startuptaskstate?view=winrt-19041

有关如何将这些任务内置到 UWP 应用中的示例,请参阅:https://windowsadmins.com/configure-your-app-to-start-at-log-in/

尽情享受吧。