Powershell

How to Extract Data from Text Files with PowerShell

PowerShell is a Windows administrator tool that is used to perform administrator-level tasks. PowerShell is such a powerful tool that it can perform the tasks that GUI(Graphical User Interface) can’t perform. Users can extract data from a text file by utilizing PowerShell’s cmdlet “Get-Content”. The “Get-Content” cmdlet is specially designed to get or extract the data from text files at the specified location. It will get the contents one line at a time.

This post will elaborate on the methods to extract data from files.

How to Extract Data via/from Text Files with PowerShell

These are the instances that will be approached to explain the extraction of data from text files with PowerShell:

Example 1: Use the “Get-Content” Cmdlet to Extract the Data via/from a Text File

First, launch the PowerShell as an administrator, then, write the “Get-Content” cmdlet along with the “-Path” parameter and assign the text file path to which the user wants to extract the data:

Get-Content -Path C:\New\Test.txt

Example 2: Use the “Get-Content” Cmdlet to Extract the Limited number of lines from a Text File

In PowerShell, to extract the desired number of lines, simply, add the “-TotalCount” parameter along with the code and assign the lines count such as “3”:

Get-Content -Path C:\New\Test.txt -TotalCount 3

Example 3: Use the “Get-Content” Cmdlet to Extract the Data from Multiple Text Files

In order to get the data of all the files in a specified folder, simply, add the asterisk “*” symbol right after the backslash of the folder name:

Get-Content -Path C:\New\*

Example 4: Use the “Get-Content” Cmdlet to Extract the Last Three Lines of a Text File

To retrieve the last three lines from a text file, first, get the file by writing the “Get-Item” cmdlet and specify the file path by using the “-Path” parameter. After that pipe the code to the cmdlet “Get-Content”. Then, utilize the “-Tail” (Use to get the last lines only) parameter and assign the value “3” to it:

Get-Item -Path C:\New\Test.txt | Get-Content -Tail 3

Example 5: Use the “Get-Content” Cmdlet to Extract a Specific Line from a Text File

First, write the code to specify the total number of lines using the “-TotalCount” parameter and wrap the code inside the small braces. Then, write a specific number within the large brackets to get the specific line:

(Get-Content -Path C:\New\Test.txt -TotalCount 5)[-3]

It can be observed that the specific line was displayed in the console.

Conclusion

To extract the data from a text file in PowerShell, the “Get-Content” cmdlet is used. To extract the data from a text file, first, place the “Get-Content” cmdlet along with the “-Path” parameter and then assign the file path. This post has elaborated on the method to extract data from text files with PowerShell.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.