Learn the basics: how to retrieve text from text files and how to find text in text files.
Use PowerShell cmdlets to learn the basics: how to retrieve text from text files and how to find text in text files.
PowerShell has many different ways to manage all of these text file types but in this article we're going to stick with the most generic approach: We'll be focusing on reading and unstructured text. This means no XML files, no SQL query files, not even PowerShell scripts themselves. We'll be focusing on a two topics:
- How to retrieve text from text files.
- How to find text in text files.
How To Retrieve Text From Text Files
One of the easiest tasks is retrieving all text from an existing text file. For most text files, a PowerShell scripter can use the Get-Content cmdlet. The Get-Content cmdlet is a very popular PowerShell cmdlet that will retrieve all text from a text file specified by the Path parameter.
At it's simplest, you can pass the Path parameter with the file path to a text file as the argument to the Get-Content cmdlet. Let's say I have a file called MyText.txt. In this text file I have the three line shown below and I'd like to get the contents of MyText.txt with a PowerShell script.
To do this, I can use Get-Content with the Path parameter.
You'll see that the result was the entire contents of MyText.txt. But what if you just wanted to see a particular line number? By default, Get-Content reads every line in a text file and creates an array as its output with each line of the text as an element in that array. This means I can easily pick out different elements in that array by specifying the array index number. In this instance, the array index number is equal to the text file line number.
In the example above, I'm showing you that the result of Get-Content is a type of System.Array and that by using index numbers in brackets I can pick and choose which lines I want output to my console. One thing to note is that if you're unfamiliar with arrays and their numbering scheme, the very first element in an array starts with a zero. This is why I'm able to use the number one to pick the second line and the number two to pick the third line. This is important to remember.
How To Find Text In Text Files
Once you're able to figure out how to find all text in text files or certain line numbers, another popular tasks is finding specific text inside of the files themselves. There are a few different ways to do this but the easiest is by using the Where-Object cmdlet. The Where-Object cmdlet is a popular cmdlet that allows you to "filter out" various information from the output of other cmdlets; in this example, it is filtering info from the Get-Content cmdlet.
Using our previous text file as an example, let's say I want to see if that text file contains the words "text file" together in it. To do this, I'll use Where-Object.
When using Where-Object I must use the $_ variable. This is a special variable in PowerShell known as the pipeline variable. In this example, it represents each line of the text file as it comes from Get-Content. I'm then using the like operator and using wildcards to see if the string "text file" exists in any of the lines of the text file. It looks like it found two lines matching the string.
If you'd like to learn more about finding text in text files I suggest you look into PowerShell's comparison operators. Using the match operator, for example, uses regex, which is much more powerful than the like operator and allows you to find just about any pattern you can think of in a text file.
We just barely scratched the surface as to what can be done with reading text files with PowerShell. I highly encourage you to reference the content on TechNet on using the Get-Content cmdlet and explore the various parameters you can use.