Sunday 28 February 2021

Using Windows Terminal and customising it

As a system administrator, you will most likely have multiple command line terminal tools that you use to help manage your systems. By the end of the day, you will end up with a screen full of terminals like the one below 
You can see that there are multiple windows for PowerShell, putty, cmd. It gets messy and can you get lost over time which window is for which especially if you work in multiple environments i.e., production, pre-production etc.

Here comes……. Windows Terminal from Microsoft which is an open-source project and you can contribute to it too if you fancy. It’s an application where you are able to open multi shells and keep them as tabs instead of multiple windows. You can think of it like your web browser where you have multi tabs and each tab is at a different location/page. You can download this application from Microsoft store and the system requirements are simple - Windows 10 version 18362.0 or higher

Once installed upon your first launch you will see that it defaults to PowerShell. You can see a “+” icon and selecting it you will get another PowerShell window as a second tab.

If you click the down arrow you will see that you can pick from the default three shell terminal window that you can open. As you select each one it will open a new tab for you.

Here I am going to show you how I customised it to make it work more for myself using 1.5.10271.0 of Windows Terminal

Adding another command shell

In this first example I am going to add another option to the list of terminals which is to open into bash for me to issues git command. (This is assuming you have already installed git-SCM). On the menu/tab bar click on the down arrow and go to settings. 

Notepad should open with the file named “settings.json”. Locate the “list” section

You should see the three default tab options for windows terminal which are PowerShell, Azure Cloud shell and Command prompt. We are now going to add our bash one and the format required at minimum is like below
We are going to add the block next on the “list” section and as it is a JSON format file we need to ensure we add “,” on the previous block. If you already have the path to the executable in your Windows system environment then on the command line you can just type in the executable name. If you don’t have it in your Windows system environment variables then you will need to point it to the path of your executable. For any “\” you will need escape them by using “\\”. You can see in the example below

Save the file and close the settings.json file. If there are any syntax errors you will see a screen similar to this one which will highlight whereabouts your error is. If you click “OK” it could still work depending on where your error was and every time you launch Windows Terminal you will receive the error message until you fix it.
If you have got the syntax correct you should now have another terminal to select from which I have labelled as “Bash Demo”
When you execute it you should see it load into a bash shell


Changing Starting Directory

As you start each of the shell window you will notice that they all start at varies places based on how you started Window terminal. Now we might want to change this because you might want PowerShell to start in the directory where your scripts are held. To make the change add “startingDirectory” within one of the lists/terminal blocks. For example, below I have added the starting directory for my PowerShell terminal to be at c:\scripts
Remember for traversing directory’s you will need to use “\\” to escape the path correctly. Once you run the PowerShell terminal you will see that the working/starting directory has changed to the one I have specified.

Passing command to shell on start-up as well

Another tip is that not only you want the command shell but you also want to pass on some commands. For example, I want to be able to connect to my Azure subscription or one of my VMware vCenter servers. Obviously, you don’t want to include your username or password on the system but at least you can pass one your Azure subscription ID or vCenter name so that you have less commands to type. On the menu/tab bar click on the down arrow and go to settings which notepad should open up settings.json. Locate the "list section"
We will add a new block after the “Bash Demo” one. For PowerShell to execute a command and not close the windows immediately we need to issue “-NoExit”. We know that to connect to an Azure subscription we use the following Azure PowerShell cmdlet “Login-AzAccount” and to pass on the subscription ID we use “-SubscriptionId” So the full command would be “powershell.exe -NoExit -Command LoginAzAccount -SubscriptionId %YourSubscriptionID%”
For my VMware one it would be similar to the command below
So overall my list section in the settings.json file is as per below;
As you can see you can do quite a bit of customisation and make it work for you. The following location at Microsoft has more information on Windows Terminal and how to customise it more (https://docs.microsoft.com/en-gb/windows/terminal/customize-settings/startup)



No comments:

Post a Comment

New Azure KMS IP and domain Addresses for activation

For Windows virtual machines deployed into Azure using marketplace images you may have created rules in your NSG or firewalls to allow the s...