Powershell can be used to read the content of a text file. There are multiple ways in which you can read and manage the content of a text file. Below, we look at various methods to read a text file in Powershell.
Powershell can be used to:
- read full content of the text file
- read specific lines of text
- read a specific line of text from the text file
We will look at the various ways in which we can read the content of a text file below.
Read the full text of text file using Powershell
Get-Content cmdlet can be used to read the full text of a text file in Powershell. In its simplest form, Get-Content can be used to display the content of a text file on the screen. The command to display content of a text file is given below:
Get-Content Path location of text file
So, we will apply this command to display the content of a text file test-text.txt using the command below:
get-content d:\test_folder\test-text.txt
You can replace the path of a text file with the path of a text file that needs to be searched. The command output of this Get-content cmdlet is displayed below as a screenshot. You can see that the command output contains the full content of the text file.
Read text from the top of a text file using Powershell
Powershell can be used to read the first few lines of a text file. For this, we will still use the Get-Content. However, we will qualify the cmdlet to start reading from the top of the file. We will also specify the number of lines that need to be read from the top of the file.
For our example, we will read the first 5 lines of a text file. The command for this function is given below.
Get-content d:\test_folder\test-text.txt | Select-object First-5
The output of this command will display the first 5 lines of the text file. The output of the file is displayed below in the screenshot.
Read text lines from the bottom of the text file using Powershell
Similar to reading text from the top of the text file, you can also read text from the bottom of the file. For our example, we will read the last 5 lines of the text file and display the output on the screen. We will be using the Get-content directive and qualify it to bring up the last 5 lines in the output.
The output of this command shows the last 5 lines of the text file.
Select a specific line of text from a text file in Powershell
We can use Powershell to pick a specific line of text from a text file. For this to happen, we will place the content of the text file in a variable and use the array positions to pick the specific line number from the text file.
From our example text file of 20 lines, we will use the Powershell commands to fetch the 14th line. Each line of text in the text file will reside in an array. The array starts from position 0. So, for a text file, array position 0 corresponds to the first line of text, and array position 19 corresponds to the 20th line of text.
The set of commands that we will use is shared below for your ready reference:
- $textfile=Get-content d:\test_folder\test-text.txt
- $textfile[13]
These two commands will store the content of test-text.txt file in a variable $textfile. The subsequent command will display the content of the text file at the array position that corresponds with the 14th line of text in the file. textfile[13] corresponds to the 14th line of text in the text file.
The output of this command is shared below as a screenshot.
An alternate and simpler way to pick a specific line of text from a text file is by using the index switch option. For example, if you wish to pick the 7th line of text from a text file in Powershell, we will use the following command:
Get-Content d:\test_folder\test-text.txt | Select -Index 6
The output of this command is represented in the screenshot below.
To read the 7th line, we qualified the Get-Content cmdlet with ‘Select -Index 6’ switch. On a similar note, if you were to read the 10th line of text from a text file, you will use the switch ‘Select -Index 9’.
This approach simplifies things a wee bit as you are not going to read the file contents in a variable and then use the array positions to display text or store text in a specific file for further operations.
Summary
In this Powershell tutorial, we learned ways to read the full text from a file, the first few lines of text from a text file, the last few lines of text from a text file, and specific lines of text from a text file. For all these text readings from a text file, we have used the Get-Content cmdlet in Powershell.
Rajesh Dhawan is a technology professional who loves to write about Cyber-security events and stories, Cloud computing and Microsoft technologies. He loves to break complex problems into manageable chunks of meaningful information.