5 minutes
Update Portable VSCode
Introduction
With development of the PowerShell ISE stopping with version 5.1, I went ahead and took the plunge and switched to Visual Studio Code (VSCode) for my development environment. I quickly became enamored with plugins and themes and all the other goodness that you find in VSCode.
The VSCode development team put out 2-3 version updates each month. Most of the time updating only involves clicking on the gear icon in the lower left corner and choosing “Restart to Update (1)”. If you don’t have administrator access or any number of other reasons why you can’t use the installer version, you can use the zip version.
If you’re not familiar with the zip file version, it’s the same program, just packaged differently. Download the file, unzip it, double click code.exe
and you’re up and running.
I prefer to run VSCode in the portable configuration so I can have all my stuff in one place. To run in portable mode, you need to create a data
folder where you unzipped your copy of VSCode. With out the data folder, VSCode will store settings in your $ENV:APPDATA
folder.
After the first time you update to a new version you see the problem with this method. Ok, not problem so much as inconvenience. The zip install doesn’t have any of your data folder with all your themes and plugins and workspaces. I like to put my VSCode install in a standard folder so I can have a taskbar shortcut.
The Old Way
The routine I would use whenever I downloaded an update was:
- Close VSCode.
- Rename VSCode folder to version it contained. (dang, what version was I running?)
- Reopen VSCode to find out the version.
- Close VSCode again.
- Rename VSCode to old version number.
- Unzip new VSCode into
VSCode
folder. - Move
data
folder from old version toVSCode
. - Reopen VSCode and get back to coding.
The PowerShell Way
My general rule for automating something random is if I have to do it more that twice, it needs a script. When I started this post, the script I used was minimal and I saw lots of room for improvement.
This is a Windows specific script.
First we’ll create a function and add a couple of parameters. I added a default for my folder.
function Install-NewVSCode {
param(
[string]$VSCodeZipFile,
[string]$InstallPath = 'E:\Users\JeffH\VSCode'
)
Next we remove the trailing backslash if it’s present and check that the folder is there.
$InstallPath = $InstallPath.TrimEnd('\');
if (Test-Path $InstallPath){
if(Get-WmiObject Win32_Process | Where-Object {$_.Path -eq "$InstallPath\Code.exe"}){
Write-Host "VSCode appears to be in use in that folder. Please close and try again.";
return;
}
}else{
Write-Host "The path $InstallPath is not valid.";
return;
}
Test that the zipfile is there and parse the version number from the file name..
if (Test-Path $VSCodeZipFile) {
# set file from param
$ZipFile = Get-Item $VSCodeZipFile
# split off version number from the filename.
$zipver = $zipfile.basename.split('-')[-1]
}
In the resources\app
folder there is a package.json
file that contains the version of that install.
if (Test-Path "$InstallPath\resources\app\package.json"){
$version = (get-content $InstallPath\resources\app\package.json | ConvertFrom-Json).version
Now before we do anything else we check to make sure we’re actually installing a newer version.
# $version from json and $zipver from file name.
if ($version -eq $zipver) {
write-host "This version is currently installed";
return;
}
elseif ($version -gt $zipver) {
Write-Host "Zipfile contains version older than currently installed";
}
Once we make it this far, the files and paths are legit and the zip version is newer than the version installed. With that we can now rename the current folder to the old folder (VSCode -> VSCode-1.59.1)
else {
Write-Host "mv $InstallPath $InstallPath-$version\";
try {
Move-Item $InstallPath $InstallPath-$version\
} catch {
Write-Host "Can not move folder";
$error[0].exception;
return;
}
Next step is to unzip the file into our install folder.
Write-Host "Expand-Archive $VSCodeZipFile $InstallPath";
Expand-Archive $VSCodeZipFile $InstallPath
After unzipping, we need to move the data
folder from the old version to the new version.
Write-Host "mv $InstallPath-$version\data $InstallPath\";
Move-Item $InstallPath-$version\data $InstallPath
}
If we couldn’t find the JSON file we fall to this branch and we should look again at what we passed to the function..
}else{
Write-Host "JSON file not found. Maybe not an existing VSCode install?"
return;
}
}
Conclusion
This little script didn’t take very log to write. Without any checks the script would only be 3 steps.
- Rename old folder.
- Unzip archive to new folder.
- Move
data
folder to new folder.
That’s what I started with. Then I found the version number tucked away in the json file I could use. Next I figured I could compare that to the version I’d downloaded. Finally making sure all the folders were there and that VSCode was closed.
Personally I’ve learned that if I’m putting off a routine task, that means it’s time to automate. Even if it’s something easy.
I hope you got something useful out of this. Thanks for stopping by.
TL;DR
Visual Studio Code has the option to install as a portable version with all setting contained in the install folder. I wrote a script to make updating and transferring your settings easier.
Download
Here’s the script of you want to download and play with it.
Comments powered by Talkyard.