If you are a .NET developer, it's an industry-agreed fact that making use of the CLI can make you a lot more productive. Since .NET 5 was released (which included the .NET CLI) it is more important than ever for .NET developers to understand how to use the terminal efficiently.

I typically make use of the terminal every day for development, as I find certain tasks much quicker than using the UI. Just because I love the terminal, I appreciate not every developer I've encountered feels the same way.

When I normally encounter a dev who avoids the terminal, the most frequent reason I've heard for people holding this view is that the terminal seems hard to master, which is not true. Whenever I've dug deeper into why they hold this belief, a common theme emerges they simply haven't configured the terminal in a way that makes it quick and easy for them to use. If any of this sounds familiar to you, that is about to change.

Within this tutorial, I will cover 6 tips that will make using the terminal easy. Implementing these tips will allow you to write code quicker, making you more productive. If that sounds good to you, read on

Essential Powershell Tips

Before we get to the more complex scripts, you first need to understand 3-4 basic commands to get you going. These commands are nothing new or special., however, when combined they will allow you to do much more complex and powerful things:

Command Chaining: The first command that you need to be aware of is command chaining. One way to become more productive on the command line is to run multiple commands at the same time. Maybe you want to do a clean and build at the same time, or run some tests and then build your project.

In terms of chaining, the operator that you will need to use will be determined by the type of shell your script will be running in. If you want to run a command within the normal Windows Terminal then there are two chain commands that you need to know about.

The first is the single ampersand operator &. The important thing to understand about the single ampersand is that if the first command fails the second command will run regardless. If you want to ensure the second command ONLY runs you can use double ampersands, &&. '&&' means "execute command 1, and if it succeeds, execute command 2"

You are free to use & and && to chain as many commands as you want and below is an example of how this might look:

Alternatively, if you need to chain commands within a Powershell script (scripts within a .ps1 file extension), you can also chain commands by either separating commands using the semi-colon just like in C#:

Alternatively, you can chain commands using the pipe operator ('|')!

Aliases: Now we can chain commands together, the next challenge with the terminal is around the time it takes for you to type these long-ass commands out.

This is why the next tip is to make use of scripts, also known as an alias, to trigger commands rather than manually typing everything out yourself. While it is possible to define an alias each time your terminal loads using a command like set-alias, a better approach is to automatically load a list of all of your favorite aliases whenever your terminal boots up:

To do this on Windows you will either need to create a corresponding batch file ('.bat') or a Powershell script (.'ps1') for each alias you want to create. The filename of the script will be the alias/trigger that you will use within your terminal to launch the script. After creating the file, you simply add the command you want to run when you type the alias inside of it.

Next, to make the terminal aware of all of your scripts, you will either need to create them within a folder that the terminal is already aware of or you will need to register the folder location of your scripts within your system path environment variable. You can do that like this:

Setting Alias Within Windows Path

The next thing to understand about using scripts within an alias is argument passing. You can pass arguments into your batch files by using the % operator and by supplying the argument position. So, %1 would be the first argument, %2 the second, etc.. Let us say I wanted to create a shortcut to echo a message on screen. I could call my file alert.bat. Within the body of that file, I could add this command:

You could then run the alias like this alert.bat warning, which would render this on-screen... Alert eddd. An important thing to be aware of here is that when you are not using arguments, you do not need to add the file extension when you call your alias, however, when you use arguments you DO need to supply the file extension, otherwise you will encounter an error!

Sound The next tip is to prevent you from staring at the terminal unnecessarily. When a script takes ages to run, instead of constantly having to monitor the terminal output to wait for it to finish, chain a beep to the end so the terminal can flag when the script has finished. Adding a beep in Powershell is done using this command:

The default beep is not the best noise in the world, so you can alter the pitch and length of the beep to make it less yucky. Changing the first number changes the tone, and changing the second number changes the duration.

If you are using a normal terminal, you will not have access to the Beep() function. Instead, you can generate a sound using this command instead:

Debugging: The last thing to consider before moving on is debugging. When setting things up and configuring your terminal, your changes will often not work. When this happens sadly the quickest way to reset things is often to close the terminal and re-open it

It is possible to clear the output on the terminal using cls and alternatively, if you have been playing around with the PATH variable it is often possible to reset it using this Powershell command:

Command History: Now you know all the shortcut tips to make executing your commands quicker, it's time to consider what scripts you want to run. There are several common commands every .NET developer will likely run, however, there will also be a bunch of commands that you favor.

When setting things like aliases up, I always struggle to remember my previous commands that I might want to aliases. This is where search comes in handy.

Did you know you can view your previous commands using Ctrl + R? If you like me I always seem to forget the commands I need to run you can cycle through all your old commands by repeatedly hitting Ctrl + R.

Alternatively, depending on how you have things set up the up and down arrow will let you navigate your commands as well! If tweaking your Powershell shell to make it more usable for you sounds interesting, there is a program called Oh My Posh that you can install that will help. With it, you can install plugins and add all sorts of styling-related settings. If you want to learn more about it I've recorded a set-up video that you can find here

Creating Useful .NET Developer Scripts

We have finally reached the point where we can start to think about the types of scripts that you want to add to your alias folder to speed things up. When it comes to NET-related stuff, as of .NET 5 you can use the DOTNET CLI tool. The CLI comes with these commands:

  • dotnet help: Get a list of commands you can run
  • dotnet new <templateName>: Create new project
  • dotnet build: Builds your project
  • dotnet restore: Restores your NuGet packages
  • dotnet run: Builds and runs your application
  • dotnet test: Executes the unit tests associated with your project
  • dotnet publish: Create pacakges

These commands are all good candidates for things for you to aliases. For example, you might create a batch file called di.bat whose contents look like this:

To call this command, you could simply type di into your shell. NOTE: If you want to pass arguments to the script you will need to add the file extension as well!

Time Your Build Command: In terms of creating some more advanced .NET scripts, a useful one that I use regularly is a performance command. The below command will time how long it takes your app to build. This command can be useful to test when you want to reduce build times!

Launch Chrome Without CORS: If you are a web developer and you create APIs frequently, you might bump into CORS issues. If the issues are local, you can sometimes bypass CORS errors by opening Chrome minus its security checks. You can do this by using the --disable-web-security flag. As I can never remember this argument, I create a batch file called chrome.bat and then add this command to it:

Launching Chrome from my terminal like this makes sure it's opened in developer-friendly mode!

GIT Scripts: The most common need to turn to the terminal is usually GIT-related commands. I won't cover all the standard ones here, however, I will highlight one of the more interesting ones I use. I am forever creating a bunch of test repositories on my machine to test different things out. When working on these projects, I tend to use Git Force Push a lot... yes some people might groan. If you want to force push quickly I create a batch file called gpush.bat and add this command:

While some folks will consider force push dangerous, if you know what you are doing you will be OK, however, no one is perfect. Sometimes mistakes happen.

Maybe once a year I might accidentally push something I didn't want to. When this happens, you can always get back the commit you might have overridden locally by browsing your local commit and checking out a local commit. As I only infrequently use this command I can never remember the syntax I need to use so I use an alias I named gfuck.bat, that contains this command:

As I only use this command maybe once a year, I can never remember it! Storing it as an alias will allow me to see my local commits and revert any files I lost simples :)

After all this, if you are still struggling to come up with ideas of what to script, I suggest you visit Mega Collection of PowerShell Scripts. This repo contains over 500 different Powershell scripts that you can make use of. From the main readme.md you will find links to scripts for file and folder-related tasks, application shortcuts, converting file types, media and file-related tasks, getting system information, and much more.

Happy Coding