One of the first things you do when learning a new computer language is learn how to do input and output. We learned how to do output from the tip Introduction to PowerShell. If you remember, we used the Write-Output cmdlet. Now it's time to learn how to read a file and display its output to the screen.
Let's say we have a text file named "C:\Temp\Temp.txt" and it contains ten lines. The simplest way to have these lines displayed is by using the Get-Content cmdlet. Here's what it looks like to run this cmdlet against our file: (See Figure 1.)
Figure 1. Get-Content reads a file.
What if you only wanted to display the first three lines of the file? There are different ways to do it, but perhaps the most straightforward way is to use variables. In PowerShell, a variable can be any datatype, and the variable name begins with a "$". Examples of variable names include "$myName", "$numbers", "$today", etc. You assign a value to a variable with an "=" sign, so a legal PowerShell command would be
PS> $myName = 'Barry'
With that background, we now know enough to display the first three lines of our file. We'll assign the contents of the file to a variable called $lines, and then display its first three lines: (See Figure 2.)
Figure 2. Using a variable.
Here, we assign the content of the file to the $lines variable (which is actually an array) and use the syntax "[0..2]" to access elements 0, 1, and 2 of the array. Element 0 is the first line; element 1 is the second line; and element 2 is the third line.
It's just as easy to access the last lines of the array. This is done by using negative subscripts, so $line[-1] is the last line of the array; $line[-2] is the second-to-last line of the array; etc. The follow figure shows how to display the last three lines of our file: (See Figure 3.)
Figure 3. Accessing an array using negative subscripts.
Another way to retrieve text from a file is to search for it and display the results. To search for text in PowerShell you use the Select-String cmdlet. Select-String takes a lot of switches, which you can see if you ask for help on the cmdlet, but a simple form requires that you just specify the file name and the pattern you're looking for. So if I wanted to find all the lines in my file that contain the text "line" (which would be every line) I could do it like this: (See Figure 4.)
Figure 4. Searching a file for a pattern.
Select-String is quite powerful because it searches using regular expressions. (There is another tip titled "Introduction to Regular Expressions".) So if you wanted to find all the lines that begin with an "l" and end with just one digit (i.e., all lines except line 10), it would look like this: (See Figure 5.)
Figure 5. Searching a file using a regular expression.
We've talked a lot of about reading files, so we should finish by talking about writing files. Of course, you can use the familiar ">" character to redirect your output to a file, but in PowerShell you can also use the Out-File cmdlet to send output to a file.
Let's say you wanted to get a list of all the active processes on your system. The cmdlet for doing this is Get-Process. A portion of the output from Get-Process is shown here: (See Figure 6.)
Figure 6. Partial output from Get-Process.
Note that the eighth line from the last is truncated. That line ends with "vmware-usbarbitrat...". If we were to simply send this output to a file, the file, too, would contain this truncated line. Instead, we can use Out-File's "-width" switch to indicate that we want a larger width so as to avoid the truncation. Here's our command to get the processes, pipe the output to Out-File, and specify a width for the output:
PS C:\Bat> Get-Process | Out-File C:\Temp\Temp2.txt -width 100
This tip (13507) applies to Windows 7, 8, and 10.
Like the older batch-file processor, PowerShell can accept parameters. This allows for flexibility in your script. This ...
Discover MoreWhen you need your computer's serial number you can look it up without looking on the outside of the box. Use PowerShell ...
Discover MoreYour computer has a name that is separate from your account name. You can use PowerShell to change the name of your computer.
Discover More2017-01-25 03:35:06
Barry
It's my pleasure. Are there certain PowerShell things in particular that you'd like to see tips about?
2017-01-25 03:29:15
Absolutely Ray Ross...
Have written to Barry as well...many times...this is really interesting and I found it very informative series by Barry
2017-01-21 15:46:25
Ray Ross
I'm hoping there are others like me who are enjoying this series of articles. Thanks!
Copyright © 2024 Sharon Parq Associates, Inc.
Comments