Search code examples
c#powershelluwpstartup

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


I hope to enable/disable the uwp app's startup items through powershell, but I tried to use gcim win32_startupcommand and Get-Item HKCU:\Software\Microsoft\Windows\CurrentVersion\Explorer\StartupApproved\Run and they couldn’t be found, shell:startup is also empty, it seems that uwp app is invisible and can only be seen in Task Manager->Startup, so what should I do?

I tried to use Regshot to compare the difference between before and after enabling the startup item, as follows:

Regshot compare result

In addition, I found that these registry entries did not exist before UWP or Task Manager was started, so in the end I thought this might be a very complicated solution, and I gave up doing this.


Solution

  • Here's a potential work-around until native calls to [Windows.ApplicationModel.StartupTask]::RequestEnableSync() and ::Disable() methods are achieved...

    In this example, I am toggling the state of Skype's desktop extension startup task:

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

    For more info about possible State values, see: https://learn.microsoft.com/en-us/uwp/api/windows.applicationmodel.startuptaskstate?view=winrt-19041

    For an example of how these tasks are built into UWP apps, see: https://windowsadmins.com/configure-your-app-to-start-at-log-in/

    Enjoy.