Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.
If you own the book already, login here to get free, online, searchable access to the entire book's content.
If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".
You want to get the content of a file.
Provide the filename as an argument to the Get-Content
cmdlet:
PS > $content = Get-Content c:\temp\file.txt
Place the filename in a ${}
section to use the cmdlet Get-Content
variable syntax:
PS > $content = ${c:\temp\file.txt}
Provide the filename as an argument to the ReadAllLines()
or ReadAllText()
methods to use the System.IO.File
class from the .NET Framework:
PS > $content = Get-Content c:\temp\file.txt -Raw PS > $contentLines = [System.IO.File]::ReadAllLines("c:\temp\file.txt")
PowerShell offers three primary ways to get the content of a file. The first is the Get-Content
cmdlet—the cmdlet designed for this purpose. In fact, the Get-Content
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 Get-Content
variable syntax and the ReadAllText()
method.
When working against files, the Get-Content
cmdlet returns the content of the file line by line. 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 Get-Content
cmdlet’s -Delimiter
parameter lets you provide one.
While useful, having PowerShell attach this extra information when you’re not using it can sometimes slow down scripts that operate on large files. If you need to process a large file more quickly, the Get-Content
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 one by one. 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 Get-Content
cmdlet is its streaming behavior. No matter how large the file, you’ll 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. For more information about how to effectively take advantage of PowerShell’s streaming capabilities, see Recipe 5.16.
If performance is a primary concern, the [System.IO.File]::ReadAllLines()
method from the .NET Framework returns all of the lines of a file, but doesn’t attach the additional (sometimes useful) properties to each line. This method also loads the entire file into memory before giving you access to it, so may be unsuitable for extremely large files.
When you want to deal with the entire content of a file at once (and not split it into lines), use the -Raw
parameter of the Get-Content
cmdlet:
$rawContent
=
Get-Content
c
:
\
temp
\
file
.
txt
-Raw
For more information about the Get-Content
cmdlet, type Get-Help Get-Content
. For information on how to work with more structured files (such as XML and CSV), see Chapter 10. For more information on how to work with binary files, see Recipe 9.6.
Recipe 5.16, “Generate Large Reports and Text Streams”
Recipe 9.6, “Parse and Manage Binary Files”
Chapter 10