Installing Visual Studio Code on Windows Using PowerShell
Install Visual Studio Code on Windows unattended from PowerShell: fetch the installer, run it silently, and stop it launching itself afterwards.
Visual Studio Code (VSCode) is a powerful, lightweight code editor that supports various programming languages and is highly customizable. Installing VSCode on your Windows Server can enhance your development environment. In this blog post, I’ll show you how to automate the installation of VSCode using PowerShell.
Why Automate with PowerShell?
Using PowerShell to install software ensures consistency, saves time, and can be easily integrated into larger automation workflows. Whether you’re setting up a new server or provisioning multiple machines, PowerShell can simplify the process.
PowerShell Script to Install VSCode
Below is a PowerShell script that downloads the latest VSCode installer and performs a silent installation on a Windows Server.
<#
.SYNOPSIS
This script installs Visual Studio Code on a Windows Server.
.DESCRIPTION
The script downloads the latest VSCode installer and performs a silent installation.
.NOTES
Run this script with administrative privileges.
#>
# Define the URL for the VSCode installer
$installerUrl = "https://aka.ms/win32-x64-stable" # system-wide; the -user- build installs per-account only
# Define the path to save the installer
$installerPath = "$env:TEMP\vscode_installer.exe"
# Download the VSCode installer
Write-Output "Downloading Visual Studio Code..."
Invoke-WebRequest -Uri $installerUrl -OutFile $installerPath
# Perform the silent installation
Write-Output "Installing Visual Studio Code..."
Start-Process -FilePath $installerPath -ArgumentList "/silent", "/mergetasks=!runcode" -NoNewWindow -Wait
# Uncomment to Clean up the installer file
# Remove-Item -Path $installerPath -Force
Step-by-Step Guide
Open PowerShell as Administrator:
- Right-click the Start menu and select “Terminal (Admin)”, or “Windows PowerShell (Admin)” on older builds.
Save the Script:
- Copy the script above and save it as
install_vscode.ps1.
- Copy the script above and save it as
Run the Script:
Navigate to the location where you saved the script.
Execute the script. Windows blocks unsigned local scripts by default, so run it with the policy bypassed for that one process:
powershell -ExecutionPolicy Bypass -File .\install_vscode.ps1
Explanation of the Script
Define Installer URL: The script starts by defining the URL for the latest VSCode installer.
Download the Installer: It then downloads the installer to the temporary directory using
Invoke-WebRequest.Silent Installation: The script runs the installer with
/silentand/mergetasks=!runcodearguments to ensure a silent installation and prevent VSCode from launching immediately after installation.Clean Up: Finally, the script optionally removes the installer file from the temporary directory.
Conclusion
Installing Visual Studio Code on a Windows Server using PowerShell is a straightforward process that can be automated for efficiency and consistency. This script simplifies the setup, making it easy to integrate into your server provisioning workflows. By leveraging PowerShell, you can ensure a smooth and repeatable installation process.
Feel free to adapt and expand this script to suit your specific needs, such as adding additional configuration steps or integrating it into larger automation frameworks.