24/7/365 Support

Get the Content of a File in Windows PowerShell

Problem

You want to get the content of a file.

Solution

Provide the filename as an argument to the GetContent cmdlet:

PS >$content = GetContent c:\temp\file.txt Place the filename in a ${ } section to use the cmdlet GetContent variable syntax:

PS >$content = ${c:\temp\file.txt} Provide the filename as an argument to the ReadAllText() method to use the System. IO.File class from the .NET Framework:

PS >$content = [System.IO.File]::ReadAllText("c:\temp\file.txt")

Discussion

PowerShell offers three primary ways to get the content of a file. The first is the GetContent cmdlet—the cmdlet designed for this purpose. In fact, the GetContent cmdlet works on any PowerShell drive that supports the concept of items with content. This includes Alias:, Function:, and more. The second and third ways are the GetContent variable syntax, and the ReadAllText() method.

When working against files, the GetContent cmdlet returns the content of the file linebyline. When it does this, PowerShell supplies additional information about that output line. This information, which PowerShell attaches as properties to each output line, includes the drive and path from where that line originated, among other things.

If you want PowerShell to split the file content based on a string that you choose (rather than the default of newlines), the GetContent cmdlet’s –Delimiter parameter lets you provide one.

While useful, having PowerShell attach this extra information when you are not using it can sometimes slow down scripts that operate on large files. If you need to process a large file more quickly, the GetContent cmdlet’s ReadCount parameter lets you control how many lines PowerShell reads from the file at once. With a ReadCount of 1 (which is the default), PowerShell returns each line onebyone. With a ReadCount of 2, PowerShell returns two lines at a time. With a ReadCount of less than 1, PowerShell returns all lines from the file at once.

Beware of using a ReadCount of less than 1 for extremely large files. One of the benefits of the GetContent cmdlet is its streaming behavior. No matter how large the file, you will still be able to process each

line of the file without using up all your system’s memory. Since a ReadCount of less than 1 reads the entire file before returning any results, large files have the potential to use up your system’s memory.

If performance is a primary concern, the [File]::ReadAllText() method from the .NET Framework reads a file most quickly from the disk. Unlike the GetContent cmdlet, it does not split the file into newlines, attach any additional information, or work against any other PowerShell drives. Like the GetContent cmdlet with a ReadCount of less than 1, it reads all the content from the file before it returns it to you—so be cautious when using it on extremely large files.

For more information about the GetContent cmdlet, type GetHelp GetContent. For information on how to work with more structured files (such as XML and CSV), see Chapter 8, Structured Files .

Help Category:

Get Windows Dedicated Server

Only reading will not help you, you have to practice it! So get it now.

Processor RAM Storage Server Detail
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 128 GB (SSD SATA) Configure Server
Intel Atom C2750 2.4 GHz 8c/8t 8 GB DDR3 1× 1 TB (HDD SATA) Configure Server
Intel Xeon E3-1230 v2 3.3 GHz 4c/8t 16 GB DDR3 1× 256 GB (SSD SATA) Configure Server
Intel Atom C2350 1.7 GHz 2c/2t 4 GB DDR3 1× 250 GB (SSD SATA) Configure Server

What Our Clients Say