private/Software/Install-MicrosoftVSCode.ps1

<#
.SYNOPSIS
Installs Microsoft Visual Studio Code using winget.
 
.DESCRIPTION
Ensures Visual Studio Code is available by installing it with winget when needed,
using recommended silent installer overrides.
 
.OUTPUTS
System.Management.Automation.PSCustomObject
 
.EXAMPLE
Install-MicrosoftVSCode
 
Installs Visual Studio Code 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-MicrosoftVSCode {
    [CmdletBinding(SupportsShouldProcess = $true)]
    [OutputType([pscustomobject])]
    param (
    )

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

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

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

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

        return $null
    }

    $vscodeCommand = & $getVSCodeCommand
    $wasInstalled = $false

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

        & $winget.Source install --id 'Microsoft.VisualStudioCode' -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 installation failed with exit code $LASTEXITCODE."
        }

        Update-OSDeploySessionEnvironment

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

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

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

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