Replicate VS Code Extensions Using PowerShell

What is Visual Studio Code?

From Wikipedia,
Visual Studio Code is a source code editor developed by Microsoft for Windows, Linux and MacOS.
It includes support for debugging, embedded Git control, syntax highlighting, intelligent code completion, snippets, and code refactoring.
It is also customizable, so users can change the editor’s theme, keyboard shortcuts, and preferences.
It is free and open-source,although the official download is under a proprietary license.

Read more at https://code.visualstudio.com/docs

Extensions in Visual Studio Code

VS Code extensions let you add languages, debuggers, tools, add new features and themes to your installation to support your development workflow and experience.
Visual Studio Code has support for almost every major programming language.
You can find extensions to install using the Extensions View.
You can install extensions the ones you like, and disable extensions you don’t want to use all the time.

How do I get consistent VS Code experience?

It’s already available on Windows, Linux and MacOS. So even if you move between environments and type of operating systems you can still get the same VS Code experience.
To make it even seemless it would be good if you can get the extensions also available when you move between different machines. Like the way you get your roaming profile and desktop experience on Windows operating system.

There are few extensions available to solve this problem. The two prominently found on marketplace are,

  1. Settings Sync
  2. Extension Manager

I wanted to try something simple as I often create virtual machines for testing, training labs.
I have already automated the VS Code silent install using PowerShell Remoting. To extend that effort, I wanted to install extensions as well, so that once my virtual machine(s) is/are ready I can start using VS Code with same experience as my physical laptop.

I added following lines of code which using VS Code command line and PowerShell remoing command (Invoke-Command)

# Setting up credentials for remote session on VMs from Host Machine.    
$hostCredential = New-Object PSCredential("MachineName\Administrator", ('mypassword' | ConvertTo-SecureString -AsPlainText -Force))

# Find the Visual Studio Code Extensions Installed and store in variable   
$vscodeExtensions = code --list-extensions

# Install Desired Visual Studio Code Extensions
# Using foreach to iterate through list of extensions in $vscodeExtensions.
# Using Invoke-Command installs the extension on remote computer.
Invoke-Command -AsJob -ScriptBlock {
foreach ($vce in $using:vsccodeExtensions){
code --install-extension $vce 
}
} -ComputerName MachineName -Credential $hostCredential -Verbose 

# Verify Visual Studio Code Extensions are installed on remote machine
Invoke-Command -ScriptBlock {code --list-extensions} -ComputerName MachineName -Credential $hostCredential -Verbose

This slideshow requires JavaScript.

Notes:

  • I have tried this to replicate extensions found in Visual Studio Code installed on my Windows 10 laptop to Visual Studio Code installed on Windows 2012 Server Virtual Machine.
  • I have not tested this method with all possible extensions however I feel this should work unless there are any specific dependencies (missing) of extension blocking the installation.
  • You can use the same techique to uninstall extensions from remote command and code should look like
Invoke-Command -AsJob -ScriptBlock {
foreach ($vce in $using:vsccodeExtensions){
code --uninstall-extension $vce 
}
} -ComputerName MachineName -Credential $hostCredential -Verbose

Thats’s it for now. Thanks for reading!!!

Advertisements