private/Install-SoftwareMicrosoftVSCodeInsiders.ps1

function Install-SoftwareMicrosoftVSCodeInsiders {
    <#
    .SYNOPSIS
        Installs Microsoft Visual Studio Code Insiders using winget
 
    .DESCRIPTION
        Locates code-insiders.cmd in PATH or the standard per-user and per-machine installation
        paths. When Visual Studio Code Insiders is unavailable, the function installs
        Microsoft.VisualStudioCode.Insiders with winget and silent overrides that add PATH,
        file association, and Explorer context menu integration without launching the
        application.
 
        After installation, the function refreshes the current process environment and locates
        code-insiders.cmd again. The current implementation does not gate installation with
        ShouldProcess, so WhatIf and Confirm do not prevent the winget operation.
 
    .EXAMPLE
        PS> Install-SoftwareMicrosoftVSCodeInsiders
 
        Installs Visual Studio Code Insiders when code-insiders.cmd is unavailable and returns
        its version and command path.
 
    .INPUTS
        None. This function does not accept pipeline input.
 
    .OUTPUTS
        System.String. Returns native winget output when Visual Studio Code Insiders is
        installed.
 
        System.Management.Automation.PSCustomObject. Returns product, version, command path,
        winget path, installer override, and installation status details.
 
    .NOTES
        Author: David Segura
        Company: Recast Software
        Version: 1.0.0
        Date: 2026-08-28
 
        Requires Windows and winget.
 
        Change Summary:
            - Added winget installation and command discovery for Visual Studio Code Insiders.
    #>

    [CmdletBinding(SupportsShouldProcess = $true)]
    [OutputType([pscustomobject])]
    param (
    )

    if (-not $IsWindows) {
        throw "[$(Get-Date -Format s)] [$($MyInvocation.MyCommand.Name)] Install-SoftwareMicrosoftVSCodeInsiders 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)] 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)] 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
    }
}