PowerShell Input and Output

Written by Barry Dysert (last updated December 27, 2020)

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 35 years, working in different positions such as technical team leader, project manager, and software developer. He is currently a software engineer with an emphasis on developing custom applications under Microsoft Windows. When not working with Windows or writing Tips, Barry is an amateur writer. His first non-fiction book is titled "A Chronological Commentary of Revelation." ...

MORE FROM BARRY

How to Choose between 32- and 64-bit Windows

Windows currently comes in two flavors: 32-bit and 64-bit. While there are typically some performance advantages ...

Discover More

Changing the Desktop Background

You can change the image that is displayed for your desktop background to a more aesthetically pleasing picture. There ...

Discover More

Hiding and Displaying the Volume Control

Whether it's to provide you with a bit more space on the taskbar or to simply remove a bit of "clutter" from the system ...

Discover More
More WindowsTips

Counting the Number of Files or Subfolders Using PowerShell

Do you need to determine the number of files or subfolders there are in a folder? You can use PowerShell to quickly count ...

Discover More

Using PowerShell to Find Your Wireless Signal Strength

Wi-Fi signal strength is important for faster, more consistent data transfers. You can use PowerShell to gain a more ...

Discover More

Check your PC's Uptime with PowerShell

Every time a computer restarts, it runs through processes that help its performance. You can use PowerShell to see how ...

Discover More
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}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. 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 five minus 3?

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