INGREDIENTS

Once you've managed to successfully start a Linux GUI desktop environment, you can create a Windows shortcut that can be pinned to Start or taskbar.

STEP 1 Find a Linux command line for your GUI desktop

Each Linux GUI desktop has its own startup executable and you need to find it along with its required command line arguments for WSL.

For example, you can use the following command line to launch the Xfce4 desktop on Ubuntu 18.04:

export DISPLAY=127.0.0.1:0.0; xfce4-session;







STEP 2 Create a folder for WSL related scripts and assets

It can be any folder on any drive, but for the sake of simplicity, let's create it on C: drive and name it 'wsl': c:\wsl. If you've decided to use a different folder path, please make adjustments accordingly in the next steps.

We're using a VBScript file to launch the actual batch file that sets up and starts the Linux GUI desktop. You can of course directly launch the batch file. However, we're primarily using the VBScript in order to hide the Command Prompt window that pops up whenever a batch file is executed.

STEP 3 Create a Windows batch file (*.bat) for launching your Linux GUI desktop

C:\wsl\start-linux-desktop.bat
REM ### Start X410 in Desktop Mode
start /B x410.exe /desktop

REM ### Start Linux GUI desktop
ubuntu1804.exe run "<command-to-launch-gui-desktop>"



Replace the "<command-to-launch-gui-desktop>" with the command you found in Step 1. Since the batch can be executed multiple times, you should wrap the command with an "if" statement.

For example, if you're using the Bash shell on Ubuntu, you can use its "pidof"; it returns the process ID of its command line argument. So when you're launching the Xfce4 desktop, you can check for the "xfce4-session" binary and prevent accidently launching it multiple times.

if [ -z "$(pidof xfce4-session)" ]; then export DISPLAY=127.0.0.1:0.0; xfce4-session; fi;



You can also automatically close X410 when the Xfce4 desktop is terminated by using the "taskkill.exe" command from Windows. So the full batch file for launching the Xfce4 desktop becomes:

C:\wsl\start-ubuntu-xfce-desktop.bat
start /B x410.exe /desktop
ubuntu1804.exe run "if [ -z \"$(pidof xfce4-session)\" ]; then export DISPLAY=127.0.0.1:0.0; xfce4-session; pkill '(gpg|ssh)-agent'; taskkill.exe /IM x410.exe; fi;"



"pkill '(gpg|ssh)-agent';" is added to terminate the 'gpg-agent' and 'ssh-agent' that are automatically launched by xfce4-session. You can of course permanently disable them from Xfce4 and remove the "pkill '(gpg|ssh)-agent';".

Please note that the "ubuntu1804.exe" is a unique executable alias for launching Ubuntu 18.04. If you also installed Kali Linux and want to run your command from that setup, you need to use "kali.exe". To find out the executable aliases for currently installed WSL distributions, go to Windows Settings » "Apps" » "Apps & features" » "Manage app execution aliases".

STEP 4 Create a VBScript file for executing a batch file without any flashing Console window

C:\wsl\bat-launcher.vbs
If WScript.Arguments.Count <= 0 Then
    WScript.Quit
End If	

bat = Left(WScript.ScriptFullName, InStrRev(WScript.ScriptFullName, "\")) & WScript.Arguments(0) & ".bat"
arg = ""

If WScript.Arguments.Count > 1 Then
    arg = WScript.Arguments(1)
End If

CreateObject("WScript.Shell").Run """" & bat & """ """ & arg & """", 0, False



Please note that the above VBScript automatically attaches the ".bat" file extension to its first argument and executes it as if it's a batch file in the same folder.

STEP 5 Create a Windows shortcut

For the location of the item, you should enter the following:

wscript.exe "C:\wsl\bat-launcher.vbs" "<batch-file-name>"



Please replace the "<batch-file-name>" with your batch file created in Step 3. For example, if your batch file is named "start-ubuntu-xfce-desktop.bat", you need to enter the following for the shortcut:

wscript.exe "C:\wsl\bat-launcher.vbs" "start-ubuntu-xfce-desktop"



Please note that you must not specify the ".bat" file extension and the batch file must exist in the same folder as the "bat-launcher.vbs" (i.e., 'C:\wsl' in this example).

If you want to change the icon for the shortcut, right-click the icon and select [ Properties ] » "Change Icon".

Share This Story, Choose Your Platform!