Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

Chapter 26. The Ten Most Important Cmdle... > Reading Text Files with Get-Content

26.4. Reading Text Files with Get-Content

Although Windows PowerShell can consume all kinds of data, thanks to its ability to use .NET classes as well as Component Object Model (COM) objects, simple, plain text files are still the most commonly processed data sources used in scripts. Get-Content makes working with text files extremely easy, because it automatically handles opening the file for reading and pulling in the file contents. Because the file data is automatically converted to a collection of strings, with each line being represented as its own string in the collection, going through each line of a file is effortless.

Most of the time, you'll see Get-Content being used with a single parameter, which is the name of the file to read. It's easy to assume that this Cmdlet is rather simplistic, but if you look at what Get-Content is capable of (by using the oh-so-fabulous Get-Help Cmdlet, which I cover earlier in this chapter), you'll see that this Cmdlet is capable of doing more than just reading in all the contents of a file. It has parameters to specify the encoding of the file so that it's read correctly. You can specify how many lines are read at a time so that if you're processing a file containing thousands of lines, you don't have to wait until the very end before the next command in the pipeline can start using it. You can even configure the maximum number of lines to read.


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint