I recently had the same issue, as I am working with different companies at the same time, and needed a script that could easily create a new WSL2 instance, and just as easily delete it.
Deletion by itself is not hard, as the official command
wsl --unregister <distro_name> works just fine, and gets rid of the WSL2 instances perfectly, without leaving any trace.
Creation however, can be troublesome or even tedious. My script does just this: https://github.com/IAL32/WSL2-Create-Distro
The following example creates a WSL2 instance using Ubuntu20.04 (Focal Fossa) using a previously downloaded
tarball (the example uses the following one: https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64-wsl.rootfs.tar.gz), creates a user with username
and adds it to the group
sudo. It also sets the user password to the one given in the pipeline, and the password for the root user, also given in the pipeline.
.\CreateLinuxDistro.ps1 -INPUT_FILENAME .\focal-server-cloudimg-amd64-wsl.rootfs.tar.gz -OUTPUT_DIRNAME "$env:LOCALAPPDATA\Packages\ubuntu2004-test-1" -OUTPUT_DISTRONAME ubuntu2004-test-1 -CREATE_USER 1 -CREATE_USER_USERNAME myuser -ADD_USER_TO_GROUP 1 -ADD_USER_TO_GROUP_NAME sudo -SET_USER_AS_DEFAULT myuser
Under the hood, if a new user is created it will also change its default shell from