After opening the PowerShell, make sure you have some text files with content. Let’s suppose you have a “new.txt” file in your home directory of Ubuntu 20.04 with the below 10 lines.
Let’s use the Get-Content command to get the contents of a file “new.txt” while using its file path. The command has been presented in the image. The output shows all the contents of a file on the PowerShell screen. You can say that the Get-Contents works the same as the “cat” command. The only difference is that the “cat” works in the terminal, and “Get-Content” works in the PowerShell.
You can also restrict the output on PowerShell by utilizing the Get-Content command. You have to use the “TotalCount” flag along with the total count of lines you want to display on the PowerShell. The file new.txt has a total of 10 lines. Upon using the total count of “2” we have got only 2 lines from the file, and the output has been restricted this way.
Let’s use the total count of 4 in the Get-Content command. As a return, we have got only 4 lines of content from the file “new.txt”.
We have got 7 lines from the file of 10 lines as the total count of 7 has been used in the Get-Content command shown below.
Another syntax of using the same Get-Content command is using the “Path” parameter. It is usually used to specify the long paths of certain files. The syntax for utilizing this command is presented below for the file “new.txt”.
You can also use the parameter “TotalCount” to specify the number of lines you want to get on your PowerShell screen. The output shows the 5 lines from the file new.txt as beneath.
The value content got from the “Get-Content” command can be saved to another array type variable. This variable can be used to show the type of content it holds with some other additional information. So, we have used the Get-Content instruction on the “new.txt” file and saved its contents to the variable “new”. Then a function GetType() has been utilized to get the type of a variable “new” as per the image. The output shows the information regarding content variables, i.e., name, base type, serial type, and constraints.
You can also count the total lines of code or content from a file using the same variable. You have to utilize the Count() function here. As a result, we have got 10 as a line count of file new.txt.
You can also demonstrate the content of a file new.txt with the help of the same variable. Simply add the content of a new.txt file into the variable “new” using the Get-Content command as done above. Execute the variable “new” in the PowerShell solely. You will get the contents below.
The Get-Content PowerShell command can also be used to get the single line from the specified file. You only have to specify the line number or “index of the line” within the command. The line number works the same as the index of an array, i.e., starts from 0 to n-1. So, you have to use the same Get-Content command with the same syntax. The only difference here is to put simple brackets around this instruction and specify the line number after it within the square brackets as per the image below.
You can see we have specified the index 0, 3, 7, and 9 to get contents from the file new.txt. The 0 index line returns the first line from file new.txt. Index 3 returns the 4th line, index 7 returns the 8th line, and index 9 returns the 10th or last line from the file.
When we have searched for index 10, it returns nothing. The index 10 represents the 11th line from the file new.txt. As the file has n= 10 lines, therefore it comes out this way.
You can also utilize negative indexing to get the lines from the file. The index 0 will remain 0. Line 10, i.e., index 9 can be written as index “-1”. Line 4, i.e., index 3 can be written as “-7”. Line 2, i.e., index 1 can be written as “-9”. The output lines for negative indexing can be seen below.
Here comes the parameter “tail”. It will be used to get the lines of contents of a file from the end. So, let’s have a look at this parameter as well. We have used the Get-Content command on the new.txt file while the parameter “Tail” has been specified. The Tail parameter uses 1 as its value. This means it will get only 1 last line from the file end. As a result, the 10th line has been displayed on the PowerShell screen.
When we add value 2 as a value to parameter “Tail”, it returns the last two lines from the file end. The output has been displayed below.
When we have specified the value of parameter “tail” as 5, it returns the 5 lines from the end of file “new.txt” as per the output image below.
This article contains the working of a Get-Content command on the PowerShell. We have used it to get the whole content lines from the file, got the single lines using positive and negative indexing, got the last lines using the tail parameter, and many more. We will be pleased if the guide helps you in any aspect.