Setting Visual Studio Code to Auto-Update in the Background

Fri, Feb 3, 2017 2-minute read

Visual Studio Code has a built-in feature to check for and install updates, but I’ve always been frustrated by having to acknowledge the update, allow the browser to restart, watch an installer, and then get back to what I was about to do anyways (which is edit some text). As a solution, here’s a quick little PowerShell script to run. It will create a background task to run every night at 3:14 AM and update VS Code for you automatically if one is available. If you tend to leave your editor open without saving files, you might want to enable the VS Code settings of files.AutoSave, and files.hotExit. When VS Code comes back up after an update, you’ll have to reopen any files that were previously open. Once VS Code gets more fully-featured crash recovery functionality, that annoyance will go away. And the PowerShell:

Register-ScheduledJob -Name VSCode_Updater -Trigger (
    New-JobTrigger -Daily -At 3:14) -ScheduledJobOption @{ RunElevated = $true } -ScriptBlock {
    $tags = Invoke-RestMethod https://api.github.com/repos/Microsoft/vscode/tags
    $latest = $tags | Foreach-Object { try { [version] $_.Name } catch { } } |
        Sort-Object -Desc | Select-Object -First 1
    $current = [Version] ((
        Get-Content 'C:\Program Files (x86)\Microsoft VS Code\resources\app\package.json' |
        ConvertFrom-Json).Version)
    if($latest -gt $current)
    {
        $codeWasRunning = @(Get-Process -name Code -ErrorAction Ignore).Count -gt 0
        if($codeWasRunning)
        {
            Stop-Process -name Code
        }
    
        Invoke-WebRequest https://vscode-update.azurewebsites.net/latest/win32/stable -OutFile $env:TEMP/vscode_latest.exe
        & $env:TEMP/vscode_latest.exe /verysilent
        if(-not $codeWasRunning)
        {
            Stop-Process -Name Code
        }
    }
}
$task = Get-ScheduledTask -TaskName VSCode_Updater
$task.Principal.LogonType = "Interactive"
$task | Set-ScheduledTask