Skip to content

Instantly share code, notes, and snippets.

@mavaddat
Last active August 9, 2021 18:26
Show Gist options
  • Save mavaddat/1c03caf35484fe88f0da82481ec4a90f to your computer and use it in GitHub Desktop.
Save mavaddat/1c03caf35484fe88f0da82481ec4a90f to your computer and use it in GitHub Desktop.
Fix VS LiveShare extensions using completely new install. This is a more aggressive, hard reset than just uninstalling and reinstalling because it also recursively deletes the extension cache and global storage directories before reinstalling.
# Get the VS Code batch script
$vsCodeCmd = Get-Command -Name code*cmd -CommandType Application -TotalCount 1
# Verify code sign
$vsCodeExe = Resolve-Path "$(Join-Path ($vsCodeCmd| Get-Item).Directory '..')\Code*.exe"
if( $null -eq $vsCodeExe -or ((Get-AuthenticodeSignature -FilePath $vsCodeExe -ErrorAction Stop).Status -ne [System.Management.Automation.SignatureStatus]::Valid)){
throw "Could not find valid VS Code Executable."
}
# Collect all the liveshare extensions
$liveShareExts = (&($vsCodeCmd) @('--list-extensions') | Where-Object -FilterScript { $_ -match 'vsliveshare' })
# If liveshare isn't installed (e.g., if this script were run twice in a row), just install the liveshare extension
if (($null -eq $liveShareExts) -and ($null -ne $vsCodeCmd)) {
&($vsCodeCmd) --install-extension 'ms-vsliveshare.vsliveshare'
} else { # Uninstall extension and remove associated extension folders, files
foreach ($liveShareExt in $liveShareExts) {
&($vsCodeCmd) --uninstall-extension $liveShareExt
if ($null -ne ($pathToRemove = Resolve-Path "$env:USERPROFILE\.vscode*\extensions\$liveShareExt*\" -ErrorAction Stop) -and (Test-Path $pathToRemove -PathType Container )) {
Remove-Item -Path $pathToRemove -Recurse -Confirm -ErrorAction Ignore
}
if ($null -ne ($pathToRemove = Resolve-Path "$env:APPDATA\Code*\User\globalStorage\$liveShareExt\" -ErrorAction Stop) -and (Test-Path $pathToRemove -PathType Container) ) {
Remove-Item -Path $pathToRemove -Recurse -Confirm -ErrorAction Ignore
}
# Reinstall the extension
&($vsCodeCmd) --install-extension $liveShareExt
}
}
@mavaddat
Copy link
Author

mavaddat commented Jul 7, 2020

If you are persistently having this issue (i.e., repeatedly resorting to using this script), it may indicate that your hard drive is failing or there is some other process corrupting the files associated with the vsliveshare extensions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment