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:
- Extract the data from a text file.
- Extract the limited number of lines from a text file.
- Extract data from more than one file.
- Extract the last three lines of a text file.
- Extract the user-specified line from a text file.
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:
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”:
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:
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:
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:
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.