Install PowerShell on Ubuntu 20.04:
As we have been working on the Ubuntu 20.04 Linux system, the first step towards PowerShell scripting is installing PowerShell on it. To install it on our Linux system, make sure to have the “snap” utility package already installed on your system because it will be used within the installation. So, as we have already installed and configured snap utility on our system, we are ready to install the PowerShell.
The “snap” instruction has been used with the keyword “install” along with the name of an application, i.e., “PowerShell –classic”. Press “Enter” to execute it. It will start downloading the “PowerShell” from some stable channel. It will take up to some seconds or minutes to conferring to your internet speed. Make sure to use an established internet connection.
After the download, the installation will be completed, of course. The output for the successful download is something like shown below in the snapshot.
Now, within the Linux terminal shell, you can use the Powershell tool. For that, you can use a single word command to initiate it, i.e., “push” and press Enter to execute. The output for the execution of this command shows that the PowerShell is ready for use.
Comment a Single Line:
We are starting our first example to comment out a single line code in PowerShell installed in Ubuntu 20.04. Firstly, we will see a code without commenting out. Therefore, we have started a code by initializing a variable “var” with value 2. In the next line, we have utilized the “echo” statement to print out the value of variable “var”. After executing the two-line code, we got an output “2” as demonstrated in the picture as well.
Let’s use the same code with commenting; it’s one line. So, we have initialized the same variable “var” with value 2. On the next two consecutive lines, we have used two “echo” statements. The first statement has been commented out, while the other has been used without the “#” sign. The commented one doesn’t show any output upon execution while the other one is showing the value of a variable “var” as 2. This is how the single line of code can be commented out in PowerShell with the “#” sign.
Let’s take a look at another example. So, we have declared two string-type variables v1 and v2. But, we have commented out both of them separately using the “#” sign before each. While, when we used the echo statement to display the values of both the string variables on the shell, it didn’t display anything. This is because the variables are commented out and hence don’t initialize as the normal ones do. This is how more than one line can be commented out separately in the PowerShell script.
Let’s take a look at another simple example without initializing any variable. So, we have initialized a print statement. This print statement takes a single word string to print out on the PowerShell. As you can observe, it has been printed out successfully. After this, we used the same print statement while the “#” sign was used at the start. Now, the print statement has been converted into a print. As you can see, it works perfectly and doesn’t show any output for the commented-out code.
This was all about commenting out the PowerShell script. Let’s have some comments after the PowerShell script each line. So, we have created a string variable “new” and assigned it a value “great”. The comment has been used to tell a user that this is just a declaration. Another line of code has been used to print out the variable “new” value using the “echo” statement. The comment has been used once again to show that this will be used to display the value of variable “new”. The execution shows the display of the variable’s value.
Comment out Multiple Lines:
We have seen enough examples to comment out the single lines. Let’s have some simple examples to comment out more than one line in PowerShell. Within the PowerShell, the multi-line commenting has been started by a less than sign along with the “#” sign, i.e., “<#”. After this, the comments have been passed or written out. When a user gets done, it will close the comment block with the “#” sign along with the greater than sign at its end, i.e. “#>”. So, below is the syntax example of using a simple block of comments on the PowerShell.
Let’s take a look at commenting out the block of code on the Powershell. So we have started the code with the “<#” sign and added a variable “var” with the value “Comment”. On pressing enter, it will automatically jump to the next line of the comment block. Then we have used the echo statement to display the value of variable “var”. After pressing “Enter”, we have jumped to the next line of the comment block. There, we have ended the comment block using the “#>” sign. This will lead us to nothing as it was all a comment block. So, there is no output.
In this guide, we have discussed a simple and easy method to do PowerShell commenting. We have discussed the installation of PowerShell on the Ubuntu 20.04 system. Along with that, we have discussed the single-line comments and multi-line or block commenting in PowerShell. As all the methods and examples were quite easy, we expect this article will be helpful a lot.