PowerShell Input and Output

by Barry Dysert
(last updated January 2, 2017)

3

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.

Author Bio

Barry Dysert

Barry has been a computer professional for over 30 years, working in different positions such as technical team leader, project manager, and software developer.  He is currently a senior software engineer with an emphasis on developing custom applications under Microsoft Windows. ...

MORE FROM BARRY

Resizing the Recycle Bin

The Recycle Bin is a great feature that allows you to recover files that you didn't intend to delete. But you may find its ...

Discover More

Modifying the Number of Jump List Items

Jump lists can be a helpful tool to quickly access common files and commands. If you'd prefer to display a different number ...

Discover More

What is the Purpose of the Forwarded Events Event Log?

The Forwarded Events event log collects events that have been forwarded from other computers. In this way you can login to ...

Discover More
More WindowsTips

Creating a Simple Batch File

One of the powerful capabilities built into Windows is the ability to create and use batch files to perform a wide variety of ...

Discover More

Using Batch Files, Part 3: The IF Command

This tip is part of a series that talks about Windows batch files. It introduces a few more commands you can use in your ...

Discover More

Introduction to PowerShell

PowerShell is found on all modern Windows computers. You might think of it as the next step up from the command line .bat ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 8Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 5 + 0?

2017-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

Shreepad S M Gandhi

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!


Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in WindowsTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.