private/Install-SoftwareMicrosoftVSCode.ps1
|
function Install-SoftwareMicrosoftVSCode { <# .SYNOPSIS Installs Microsoft Visual Studio Code using winget .DESCRIPTION Locates code.cmd in PATH or the standard per-user and per-machine installation paths. When Visual Studio Code is unavailable, the function installs Microsoft.VisualStudioCode 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.cmd again. The current implementation does not gate installation with ShouldProcess, so WhatIf and Confirm do not prevent the winget operation. .EXAMPLE PS> Install-SoftwareMicrosoftVSCode Installs Visual Studio Code when code.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 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. #> [CmdletBinding(SupportsShouldProcess = $true)] [OutputType([pscustomobject])] param ( ) if (-not $IsWindows) { throw "[$(Get-Date -Format s)] [$($MyInvocation.MyCommand.Name)] Install-SoftwareMicrosoftVSCode 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)] 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)] 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 } } |