Skip to content

Instantly share code, notes, and snippets.

@karthiks
Created September 6, 2020 08:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karthiks/82a6a6e2f2fb4ff69ce91ceb3f62a086 to your computer and use it in GitHub Desktop.
Save karthiks/82a6a6e2f2fb4ff69ce91ceb3f62a086 to your computer and use it in GitHub Desktop.
# Function to change the default user
Function WSL-SetDefaultUser ($distro, $user) { Get-ItemProperty Registry::HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss\*\ DistributionName | Where-Object -Property DistributionName -eq $distro | Set-ItemProperty -Name DefaultUid -Value ((wsl -d $distro -u $user -e id -u) | Out-String); };
# Usage
# WSL-SetDefaultUser <DistroName> <UserName>
$title = 'WSL Import'
$question = 'The current Ubuntu WSL system will be unregistered, and the new VHD imported. Are you sure you want to proceed?'
$choices = '&Yes', '&No'
$decision = $Host.UI.PromptForChoice($title, $question, $choices, 1)
if ($decision -eq 0) {
Write-Host 'Continuing with deregistration of Ubuntu, and import of replacement VHD'
wsl --unregister Ubuntu
Write-Host 'Importing VHD...'
wsl --import Ubuntu D:\VMs\WSL\Ubuntu\ D:\Temp\ubuntu.tar --version 2
Write-Host 'Setting default user...'
WSL-SetDefaultUser Ubuntu tony
} else {
Write-Host 'Operation canceled.'
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment