private/Software/Install-MicrosoftVSCodeInsiders.ps1

<#
.SYNOPSIS
Installs Microsoft Visual Studio Code Insiders using winget.
 
.DESCRIPTION
Ensures Visual Studio Code Insiders is available by installing it with winget
when needed, using recommended silent installer overrides.
 
.OUTPUTS
System.Management.Automation.PSCustomObject
 
.EXAMPLE
Install-MicrosoftVSCodeInsiders
 
Installs Visual Studio Code Insiders if it is not already available.
 
.NOTES
Author: David Segura
Company: Recast Software
This function is supported only on Windows and requires winget.
Change Summary:
#>

function Install-MicrosoftVSCodeInsiders {
    [CmdletBinding(SupportsShouldProcess = $true)]
    [OutputType([pscustomobject])]
    param (
    )

    if (-not $IsWindows) {
        throw "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Install-MicrosoftVSCodeInsiders is supported only on Windows."
    }

    $winget = Get-Command -Name 'winget' -ErrorAction SilentlyContinue
    if (-not $winget) {
        throw "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] winget is required but was not found. Install App Installer from Microsoft Store and try again."
    }

    $installerOverride = '/SILENT /mergetasks="!runcode,addcontextmenufiles,addcontextmenufolders,associatewithfiles,addtopath"'

    $getInsidersCommand = {
        $command = Get-Command -Name 'code-insiders' -ErrorAction SilentlyContinue
        if ($command) {
            return $command
        }

        $fallbackPaths = @(
            (Join-Path -Path ${env:LOCALAPPDATA} -ChildPath 'Programs\Microsoft VS Code Insiders\bin\code-insiders.cmd'),
            (Join-Path -Path ${env:ProgramFiles} -ChildPath 'Microsoft VS Code Insiders\bin\code-insiders.cmd')
        )

        foreach ($fallbackPath in $fallbackPaths) {
            if (Test-Path -Path $fallbackPath) {
                return Get-Command -Name $fallbackPath -ErrorAction SilentlyContinue
            }
        }

        return $null
    }

    $insidersCommand = & $getInsidersCommand
    $wasInstalled = $false

    if (-not $insidersCommand) {
        Write-Host "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Visual Studio Code Insiders is not installed. Installing with winget..." -ForegroundColor DarkGray

        & $winget.Source install --id 'Microsoft.VisualStudioCode.Insiders' -e -h --override $installerOverride --accept-source-agreements --accept-package-agreements
        if ($LASTEXITCODE -ne 0) {
            throw "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Visual Studio Code Insiders installation failed with exit code $LASTEXITCODE."
        }

        Update-OSDeploySessionEnvironment

        $insidersCommand = & $getInsidersCommand
        if (-not $insidersCommand) {
            throw "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Visual Studio Code Insiders was installed but code-insiders was not found in PATH. Open a new PowerShell session and run the command again."
        }

        $wasInstalled = $true
    }
    else {
        $installedVersion = (& $insidersCommand.Source --version | Select-Object -First 1).Trim()
        Write-Host "[$(Get-Date -format s)] [$($MyInvocation.MyCommand.Name)] Visual Studio Code Insiders is already installed: $installedVersion" -ForegroundColor Green
    }

    $finalVersion = (& $insidersCommand.Source --version | Select-Object -First 1).Trim()

    [pscustomobject]@{
        ProductId         = 'Microsoft.VisualStudioCode.Insiders'
        Version           = $finalVersion
        WasInstalled      = $wasInstalled
        CommandPath       = $insidersCommand.Source
        WingetCommand     = $winget.Source
        InstallerOverride = $installerOverride
    }
}